Advertisement
sriyanto

EggSpwans

Jun 1st, 2023
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.75 KB | Source Code | 0 0
  1. local eggs = game.ReplicatedStorage.Eggs
  2. local spawns = game.Workspace.EggSpawns
  3. while wait(2) do --to select the random egg and spawn every 2 seconds to put it out in the location
  4.     --anything inside this while loop will keep on running for every two seconds
  5.     --create a table for all of the eggs that are present in the folder
  6.     local eggsTable = eggs:GetChildren() --This will help in getting all of the eggs in the folder
  7.  
  8.     --random egg pick out of the table
  9.     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
  10.  
  11.     --create a table to choose the random spawn
  12.     local spawnsTable = spawns:GetChildren()
  13.  
  14.     --to pick a random spawn from the table
  15.     local randomSpawn = spawnsTable[math.random(1,#spawnsTable)]
  16.  
  17.     --After getting a random egg, create a clone of it to not use the egg which is their in replicated storage
  18.  
  19.     local eggClone = randomEgg:Clone()
  20.  
  21.     --to put the cloned egg in a new folder
  22.     eggClone.Parent = game.Workspace.SpawnedEggs
  23.  
  24.     --to set the position of the cloned egg
  25.     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
  26.     eggClone.Anchored = false
  27.  
  28.     --touch event when the egg has been touched by the player
  29.     eggClone.Touched:Connect(function(hit) --hit is the arguement, an object which touches the egg
  30.         --when the egg has been touched, delete it
  31.         --to check whether egg has been touched by the player or not
  32.         local playr = game.Players:GetPlayerFromCharacter(hit.Parent) --this will return true or false, depending egg got hit by the players body only
  33.         if playr then
  34.             --if true
  35.             eggClone:Destroy()
  36.         end
  37.     end)
  38. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement