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

[SOLVED] How to update a :GetChildren() variable in a PlayerAdded event?

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.

1 answer

Hi! I solved it myself! I removed the local tag in my variable to make it script-global.

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