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

Script isn't changing a value when a player joins?

I have a script that changes a value inside of replicated storage when a player joins or leaves. Here is my script:

wait(1)
local Players = game:GetService("Players") --gets players
local value = script.Parent.PlayersOnline
local PlayerCount = #Players:GetPlayers() --gets the amount of players

Players.PlayerAdded:Connect(function() --if a player gets added,
    PlayerCount = PlayerCount + 1 --increase the playercount by 1
	value.Value = PlayerCount
end)
 
Players.PlayerRemoving:Connect(function() --if a player gets removed,
	PlayerCount = PlayerCount - 1 --decrease the playercount by 1
	value.Value = PlayerCount
end)

When someone joins, the value doesn't change for some reason. Any suggestions?

4 comments

0

I dont know, bro, have you checked the Developer Console or that thing (F9) and it may say the error, maybe a wrong variable, i dont know, check that again

0

There are no errors, sorry.

0

Is this a LocalScript or a Server Script?

0

This is a script.

1 answer

If you're testing in Play Solo mode in Roblox Studio, sometimes the player loads too fast and the event isn't fired. To fix it, either create a proper local server or add a function to the event and loop through the players to not miss any of them. Example:

function onPlayerAdded(player)
--Codes here
end

for _, player in pairs(game.Players:GetPlayers()) do --looping through the players list
	onPlayerAdded(player)
end

game.Players.PlayerAdded:Connect(onPlayerAdded) -- attaching the function to the event

For the PlayerRemoving event, you can also do the same.

1 comment

0

Turns out that the script just doesn't work inside of ReplicatedStorage. Sorry about that.

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