Link Simulation: https://www.roblox.com/games/8209539876/Egg-Hunting-Game
In Workspace, create 2 Folders:
Rename become
EggSpawns (33 Spawns) and
SpawnedEggs (Empty)
Egg Template: https://www.roblox.com/library/4862332093/EggTemplate
ReplicatedStorage==> Create the Folder, rename Eggs==> download here
Setting Graphical User Interface (GUI)
ScreenGui==>MainGUI
LocalScript
Frame
ViewportFrame
textButton==>OK
textLabel==>Congrats
textLabel==>YouFound
ServerScriptService==> EggSpawns
local eggs = game.ReplicatedStorage.Eggs --variable for the eggs in replicated storage
local spawns = game.Workspace.EggSpawns --variable for spawns in spawns folder
while wait(2) do --to select the random egg and spawn every 2 seconds to put it out in the location
--anything inside this while loop will keep on running for every two seconds
--create a table for all of the eggs that are present in the folder
local eggsTable = eggs:GetChildren() --This will help in getting all of the eggs in the folder
--random egg pick out of the table
local randomEgg = eggsTable[math.random(1,#eggsTable)] --value for random will be 1 and however items are their in the table, #eggstable will give the amount of items in that table
--create a table to choose the random spawn
local spawnsTable = spawns:GetChildren()
--to pick a random spawn from the table
local randomSpawn = spawnsTable[math.random(1,#spawnsTable)]
--After getting a random egg, create a clone of it to not use the egg which is their in replicated storage
local eggClone = randomEgg:Clone()
--to put the cloned egg in a new folder
eggClone.Parent = game.Workspace.SpawnedEggs
--to set the position of the cloned egg
eggClone.Position = randomSpawn.Position + Vector3.new(0,20,0) --to get the current position of the random spawn and moving it to 20 studs y axis
eggClone.Anchored = false
--touch event when the egg has been touched by the player
eggClone.Touched:Connect(function(hit) --hit is the arguement, an object which touches the egg
--when the egg has been touched, delete it but also make the GUI appear
--to check whether egg has been touched by the player or not
local playr = game.Players:GetPlayerFromCharacter(hit.Parent) --this will return true or false, depending egg got hit by the players body only
if playr then
--if true, make the GUI appear on the screen
game.ReplicatedStorage.FoundEgg:FireClient(playr,eggClone.Name) --with fireclient we are sending a request to a certain player to do something on their end
eggClone:Destroy()
end
end)
end
StarterGui==>MainGUI==>Client (LocalScript)
local GUI = script.Parent --variable for script.parent and not just the GUI
--shortcuts to call these commands
local frame = GUI:WaitForChild("Frame") --To check whether the frame is fully loaded or not, if not wait until then
local OK = frame:WaitForChild("OK")
local VPF = frame:WaitForChild("ViewportFrame")
--Mouse button venet on OK button
OK.MouseButton1Click:Connect(function()
frame.Visible = false
end)
--create an event that will pick up whenever the found egg is triggered
game.ReplicatedStorage.FoundEgg.OnClientEvent:Connect(function(eggName)
--check whether the egg is real egga and is in certain replicated storage
if game.ReplicatedStorage.Eggs:FindFirstChild(eggName) then
frame.Visible = true
VPF:ClearAllChildren() --to remove anything if we have in VPF initially
local eggClone = game.ReplicatedStorage.Eggs[eggName]:Clone()
--after creating the egg clone, let's parent it inside the view point frame
eggClone.Parent = VPF
frame.YouFound.Text = "You Found "..eggName.." ! " --".." is a way to put the data with a string
--create the camera which will render whatever goes in the viewport
local camera = Instance.new("Camera")
camera.Parent = VPF --camera tells the VPF what we should look at to actually see the egg
VPF.CurrentCamera = camera --set the VPF current camera property to the camera
camera.CFrame = eggClone.CFrame * CFrame.new(0,0,eggClone.Size.z * 1.5) --set the cameras see frame of what it is looking at
end
end)