Roblox not running my scripts

I currently have a localscript to update a playerlist I’ve made in a GUI, I’ve added multiple print functions to see what was not working and it doesn’t even print the first line which comes right after of game.Players.PlayerAdded. No output response.

Script:

local sample = script.Parent.DisplayArea.Players.Player

game.Players.PlayerAdded:connect(function(player)

		print('player added') -- This doesn't even print
		local newgui = sample:Clone()
		print('gui cloned')
		newgui.Visible = true
		newgui.Parent = script.Parent.DisplayArea.Players:WaitForChild('ScrollingFrame')
		print('gui moved')
		newgui.Name = player.Name
		newgui.Row.Disabled = false

end)

game.Players.PlayerRemoving:connect(function(player)
	
	for i,v in pairs( script.Parent.DisplayArea.Players:WaitForChild('ScrollingFrame'):GetChildren()) do
		
		if v.Name == player.Name then
			
			v:Destroy()
			
		end
	end
	
end)
2 Likes

is your code disabled? it will not work if your scripts are disabled.
(30 chars)

is this a local script? PlayerAdded won’t fire for the local player since they’re already in the game by the time the event is connected.

And what @posatta said about already in-game players

4 Likes

If it’s a LocalScript, players are already going to be in-game by the time it connects to PlayerAdded. Try looping through existing players and calling the same function.

5 Likes

I can guess that this is a local script by the fact that there’s a gui configuration

That’s weird, I was watching a tutorial where the person used PlayerAdded, I guess I just misinterpreted, will try to do what you guys said in order to fix this.

1 Like

Also the fact that he said it was a LocalScript helps a bit.

4 Likes

That’s because the player probably joins before the event connection is created. That’s why you have to rewrite that a little bit.

local sample = script.Parent.DisplayArea.Players.Player

local addPlr = function(player)
		print('player added') -- This doesn't even print
		local newgui = sample:Clone()
		print('gui cloned')
		newgui.Visible = true
		newgui.Parent = script.Parent.DisplayArea.Players:WaitForChild('ScrollingFrame')
		print('gui moved')
		newgui.Name = player.Name
		newgui.Row.Disabled = false
end)

game.Players.PlayerAdded:Connect(addPlr)
for _,plr in pairs(game:GetService("Players"):GetPlayers()) do
    addPlr(plr)
end

game.Players.PlayerRemoving:Connect(function(player)
	for i,v in pairs( script.Parent.DisplayArea.Players:WaitForChild('ScrollingFrame'):GetChildren()) do
		if v.Name == player.Name then
			v:Destroy()
		end
	end
end)

However that solves only one problem, the second one is that you should be doing that in a server script so it needs another fix. (edit thanks to @posatta; I didn’t notice that you are creating a local player list and I thought that it is some globally visible list)

2 Likes

Just a minor nitpick, you should be using ipairs to iterate through arrays. pairs is better used with non-numeric keys (e.g. a dictionary).

Also, this shouldn’t be a server script since he’s manipulating UI, so he should be fine using that code locally.

3 Likes