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

How can i pick a random number from a table and give it to a parts position?

Asked by 2 years ago
local player = nil

game.Players.PlayerAdded:Connect(function(plr)
    player = plr 
end)

local supplycrate = game.Workspace.Items["Supply Crate"]

local Timer = 10

local function SpawnAirdrop()
    local GUI = player:WaitForChild("PlayerGui"):WaitForChild("SupplyCrate")
    local spawns = {
        Vector3.new(-83.777, 55.416, -257.739)
    }
    local newSupplyCrate = supplycrate:Clone()
    newSupplyCrate.Parent = game.Workspace
    newSupplyCrate.Position = Vector3.new(math.random(#spawns))
    print(tostring(supplycrate.Position)) 
    GUI.Enabled = true
end
wait(Timer)
SpawnAirdrop()

This is code is in a server script, I'm trying to clone a supply drop and set its posiiton to one of the positions in the spawns table. But the position doesn't seem to be working as its printing this position (-46.345863342285156, -60.3950080871582, -500.4015808105469) Which is obv not the one it should be printing. (eventually there will be more spawns but there is only one just for testing)

1 answer

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
2 years ago

The way you're referencing your table is wrong.

change this:

newSupplyCrate.Position = Vector3.new(math.random(#spawns)) -- this would give weird values just like what your getting!.

to this:

newSupplyCrate.Position = spawns[math.random(#spawns)]

Note: You may need to add another spawn entry to your spawns table to keep the math.random() function happy.

Hope this helps! :)

0
Thanks it worked! Sonnymacko 19 — 2y
Ad

Answer this question