Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do I Generate a Random Position for A Model?

So I want to make this system that clones a cage and spawns it somewhere. Here is my code so far:

local spawner = game.ReplicatedStorage.SpawnCage1
local cage = game.ServerStorage.Cage:Clone()

spawner.Changed:Connect(function()
	if spawner.Value == true then
		cage.Parent = workspace
		local position1 = Vector3.new(0,0,0)
		local position2 = Vector3.new(10,0,0)	
		local position3 = Vector3.new(20,0,0)
		local position4 = Vector3.new(30,0,0)
		local chosenPos = {position1,position2,position3,position4}
	cage.PrimaryPart.Position = math.random(1,#chosenPos)
	elseif spawner.Value == false then
		cage.Parent = game.ServerStorage
	end
end)

Here is the error in the output: 17:30:12.412 - ServerScriptService.Cage1Spawner:12: bad argument #3 to 'Position' (Vector3 expected, got number)

I would really appreciate if someone helped me out with this. Thanks in advance!

1 answer

✓ Accepted answer

Instead of

cage.PrimaryPart.Position = math.random(1,#chosenPos)

try using

cage.PrimaryPart.Position = chosenPos[math.random(1,#chosenPos)]

1 comment

0

Thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.