Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years ago
Edited 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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

Ad

Answer this question