Ok then.
It looks like you (for whatever reason) expected math.random() to give you a userdata rather than an integer.
So for starters, lets create a valid example of how to use math.random(). Say I want to print a random number from 1 through 10
num = math.random(1,10)
print(num)
or
print(math.random(1,10))
What about choosing a random userdata!??!
We're getting there, first I want to do a little briefing on tables:
tab = {}
table.insert(tab,'oof')
print(#tab)
print(tab[1])
table.remove(tab, 1)
Now we're there!
Now, if this is a script in game.ServerScriptService:
plrs = {}
game.Players.PlayerAdded:Connect(function(plr)
table.insert(plrs, plr.Name)
end)
game.Players.PlayerRemoving:Connect(function(plr)
for i,v in pairs (plrs) do
if v == plr.Name then table.remove(plrs,i)
end
end
end)
function pickrandomplayer()
local num = math.random(1,#plrs)
workspace[plrs[num]]:MoveTo(game.Workspace.spawntest)
end
Then you run pickrandomplayer() whenever you need to pick a player.
Note: You should probably research the roblox wiki before you ask questions here.
5 comments
I recommend using his script instead of mine as well as making it a function.
I used your script on 'Line 2' local Killer = player[math.random(#player)] and then came up with an error. This error said attempt to get length of local 'player' (a userdata value)
The # operator only works with a list. In my example, I stored a list of current players in the Player variable. You shouldn't be using a loop for what you're currently doing.
*Players, not Player
Thank you, It worked.