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

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

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

✓ Accepted answer

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! :)

1 comment

0

Thanks it worked!

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