I know that there is a video tutorial on ROBLOX Wiki, but it will not fix this script. It is for an odd mining game that I am making, so that a dinosaur skull will spawn in random places, but it does not work. The skull blocks do not even appear. I know that this will probably be an easy fix for experienced scripters, so please take a few seconds and help me. Here is the game so far: https://www.roblox.com/games/544182596/No-name-yet-Open-during-construction
Here is the script:
local skull = script.Parent:Clone() local xpos = math.random(-10,10) local zpos = math.random(-10,10) function skullspawn() skull.Parent = game.Workspace skull.Script:Destroy() skull.Position = Vector3.new(xpos, 0.02, zpos) end while true do skullspawn() wait(0.1) end
The problem is that xpos and zpos are also constant. The random function returns to you a number, in your between -10 and 10, so lines 2 and 3 is like saying xpos = -4 and ypos = 6, for example. You want to compute new random numbers each time the function is called. Also, skull is only ever cloned once, so the second time the function is run, 'Script' will no longer be a child of the skull. I am going to assume you want a brand new skull each time the 'skullspawn' function is called.
Solution:
function skullspawn() local x = math.random(-10, 10) local z = math.random(-10, 10) skull = script.Parent:Clone() skull.Script:Destroy() skull.Position = Vector3.new(x, 0.02, z) skull.Parent = game.Workspace end while true do skullspawn() wait(5) -- Spawn a new skull every 5 seconds end