Unique error in my script

Hello devs.
I am currently making a camping inspired game and I made a dialogue gui for my game.
So I have a problem when I play the game in roblox studio it works but when I run it in roblox it’s not working.

Here is a screeshot and the script.

function ChooseRandomPlayer()
return game.Players:GetChildren()[math.random(1, #game.Players:GetChildren())]
end

1 Like

You’re likely running this at the very beginning so no player was able to be loaded. You might want to wait for 2 players to join.

local Players = game:GetService("Players")

while #Players:GetPlayers() < 2 do
    Players.PlayerAdded:Wait()
end

-- Continue on under here
1 Like

If you aren’t already doing it, I suggest calling the function when a new player joins:

local function onPlayerAdded(player)
	 -- Your function
end

game.Players.PlayerAdded:Connect(onPlayerAdded)
1 Like

I don’t think you can do

1,#game.Players:GetChildren 

when you are by yourself testing because there is only 1 player and you can’t choose a random out of 1 as far as I know. That’s why it’s saying the interval is empty.

Technically math.random(1, 1) is fine. But the second argument was smaller than the first (1, 0) hence the exception. You can’t generate a minimum number of 1 but a maximum of 0.

1 Like

Oh my bad! I guess I’ve been thinking of it wrong all this time (Heh…) Thanks!

@sjr04 and @alikeshadow both of your answers where correct.
Thank you :happy4:

1 Like

No problem atleast tou were trying to help me. :smiley:

1 Like