So I have just started scripting in roblox and I am trying to make a simple script that will randomly select a player just like a murder mystery game! The code:
local players = game.Players:GetChildren() local imposter = nil game.Players.PlayerAdded:Connect(function() print (players) end) if #players > 2 then Imposter = players[math.random(1, players)] print(imposter) end
The output:
{} - Server - ImposterSelect:4
What I imagine is happening is that the variable is getting set before I join the game making it empty. What I am trying to do is update the variable to be what is currently is. I could put the line local players = game.Players:GetChildren()
in the PlayerAdded
event but I would need to redefine it outside the event to access it.
Hi! I solved it myself!
I removed the local
tag in my variable to make it script-global.