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.
Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

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()

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

Instead of

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

try using

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

Answer this question