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

My for _, player w'ont try to find a randomplayer ?

while wait() do
	for i = 5,0,-1 do
		--Lobby Timer
		wait(sec)
		print(i.." / Before")
	end
	for _, player in pairs(game.Players:GetPlayers()) do
		--Player Monster
		local randomplayer = player[math.random(1, #player)]
	end
end

The output say that player is a userdata value and i d'ont know how to fix this

3 answers

✓ Accepted answer

well, the reason might be that player, is well... a player object and not a table so you might want to try something on the lines of this

math.randomseed(tick())
while true do
	local plrs = game.Players:GetPlayers()
	local rIndex = math.random(1,#plrs)
	local randomplr = plrs[rIndex]
	wait()
end

So things to take away:

1) don't use while wait() do end, use while true do wait() end
2) use math.randomseed(tick()) for "better randomness"

2 comments

0

What's erong with while wait() do??

0

*wrong

If you're trying to select a random player, that's not the way to do it.

Try this:

while wait() do
    for i = 5,0,-1 do
        --Lobby Timer
        wait(sec)
        print(i.." / Before")
    end
    local players = game.Players:GetPlayers()
    local randomplayer = players[math.random(1, #players)]
end

a for loop literates through a table, and the variable is the object, not a table which you'd use a # operator to get a length. this means you dont need to use a for loop to get a random player, but just use game.Players:GetPlayers(), math.random and the # operator alone.

 while true do
    for i = 5,0,-1 do
        --Lobby Timer
        wait(sec)
        print(i.." / Before")
    end
	local players = game.Players:GetPlayers()
	local chosenPlayer = players[math.random(1,#players)]
	
	print(chosenPlayer.Name) -- example
	-- do stuff here

 	-- you dont need a wait as theres a for loop which yields
end

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