I haven’t been able to get it working. To Clone the Object randomly across the map I am not sure what to do.
part = --[[part u wanna clone]]:Clone() -- oh and put this inside the while loop
-
Get the object you want to clone
-
Get the baseplate you want the object to spawn on
-
Get the X-size and Z-size of said object
-
Get the Y height at which the object spawns
-
Get the position of the baseplate
-
Write a function to determine the random position.
local function GetRandomPos (position, Xsize, Zsize, Yheight)
local randomX = math.random(-Xsize/2, Xsize/2)
local randomZ = math.random(-Zsize/2 , Zsize/2)
local spawnAt = Vector3.new(randomX, Yheight, randomZ)
-- spawnAt is relative to baseplate position
-- Divide Xsize into left (negative) and right (positive)
-- same with Zsize because position is at center of part
return spawnAt + position
end
- Get all these things, throw it in a loop
local interval = 1
local Yheight = 100
local objTemplate = -- Your object toclone
local spawnOn = workspace.Baseplate
while spawnOn ~= nil do
local clone = objTemplate:Clone()
local randomPos = GetRandomPosition (spawnOn.Position, spawnOn.Size.X, spawnOn.Size.Y, Yheight)
clone.Parent = workspace
clone.Position = randomPos
wait(interval)
end
local rock = game.ServerStorage:WaitForChild("Rock")
while wait(math.random(1,5)) do
local cloneRock = rock:Clone
cloneRock.Position = Vector3.new(math.random(x,y), 100, math.random(x,y))
cloneRock.Parent = game.Workspace
end
Should this be seperated into 2 scripts?
Not necessary
local function GetRandomPos (position, Xsize, Zsize, Yheight)
local randomX = math.random(-Xsize/2, Xsize/2)
local randomZ = math.random(-Zsize/2 , Zsize/2)
local spawnAt = Vector3.new(randomX, Yheight, randomZ)
-- spawnAt is relative to baseplate position
-- Divide Xsize into left (negative) and right (positive)
-- same with Zsize because position is at center of part
return spawnAt + position
end
local interval = 1
local Yheight = 100
local objTemplate = -- Your object toclone
local spawnOn = workspace.Baseplate
while spawnOn ~= nil do
local clone = objTemplate:Clone()
local randomPos = GetRandomPosition (spawnOn.Position, spawnOn.Size.X, spawnOn.Size.Y, Yheight)
clone.Parent = workspace
clone.Position = randomPos
wait(interval)
end
Ah no. Put it all in one script. You want the function to be usable in the loop.
Should It be placed in the part to Clone or in serverscriptservice, workspace, Ect?
Put it in ServerScriptService/workspace.
Not in cloned part because a new loop will run when a new clone object is spawned so you get like 10+ while loop running after 10 seconds
You can put in workspace, serverscriptservice, anywhere else
You can use Instance:Clone()
. This will return an exact copy of the instance, properties and all. To spawn objects in intervals you can use RunService, a while loop, a repeat loop or a for loop, depends on your usecase.
For example, infinitely spawning objects:
local interval = 1 -- seconds
local function runLoop() -- we are creating a function so we can wrap it in a coroutine, this stops our code from yielding.
while true do -- do not use while wait() do.
wait(interval) -- interval wait, refers to your interval constant
local newObj = instance:Clone() -- this is cloning whatever instance you want, just make sure to replace instance with the actual object
-- set all the instance properies or values here, do this before parenting the part
newObj.Parent = workspace -- set parent
end
end
coroutine.wrap(runLoop)() -- run this function on its own thread (does not yield)
print("Done") -- this will print as the loop is not yielding on the current thread
Oh whoops I typo’d the function name
It’s GetRandomPos not GetRandomPosition
I corrected that typo But its still not spawning the bubbles is there something im missing?
Hmm hold up let me try it in studio
Ok so I found another typo
it’s
local randomPos = GetRandomPos(spawnOn.Position, spawnOn.Size.X, spawnOn.Size.Z, Yheight)
not
spawnOn.Size.X ,spawnOn.Size.Y, Yheight
other than that my script works fine. No error messages?
It’s already spawning a whole lot of ‘Bubbles’
Got one and that is Baseplate is not a valid member of workspace.
and in an empty project I put it in they were all just static floating in the sky.
hmm try doing
local spawnOn = workspace:WaitForChild(“Baseplate”)
(sometimes the script load faster than the game so it looks at workspace before Baseplate is added)
Also it’s floating static because you anchored them
Thank you but I just got it working. I Just added a baseplate and it works now. Thank you to Everyone who Helped.
Oh lol I thought you already have baseplate in the game. You’re welcome