New: Nitro Boost our Discord server and receive full donation perks here on the website! Join the Scripting Helpers Discord Server to learn more! You can also Support on Patreon as always.
Still have questions? Join our Discord server and get real time help.
Log in to vote

How Do I Generate a Random Position for A Model?

Asked by 10 days 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()

    if spawner.Value == true then
        cage.Parent = workspace
        local position1 =,0,0)
        local position2 =,0,0)   
        local position3 =,0,0)
        local position4 =,0,0)
        local chosenPos = {position1,position2,position3,position4}
    cage.PrimaryPart.Position = math.random(1,#chosenPos)
    elseif spawner.Value == false then
        cage.Parent = game.ServerStorage

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
Answered by 10 days ago

Instead of

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

try using

cage.PrimaryPart.Position = chosenPos[math.random(1,#chosenPos)]
Thanks. PrismaticFruits 73 — 10d

Answer this question