Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How Do I Generate a Random Position for A Model?

Asked by 4 years ago

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

Log in to vote
1
Answered by 4 years ago

Instead of

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

try using

cage.PrimaryPart.Position = chosenPos[math.random(1,#chosenPos)]
0
Thanks. PrismaticFruits 842 — 4y
Ad

Answer this question