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

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

Asked by
DemGame 271 Moderation Voter
4 years ago

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?

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 El_Tycoonero0Delgado 18 — 4y
0
There are no errors, sorry. DemGame 271 — 4y
0
Is this a LocalScript or a Server Script? proqrammed 285 — 4y
0
This is a script. DemGame 271 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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.

0
Turns out that the script just doesn't work inside of ReplicatedStorage. Sorry about that. DemGame 271 — 4y
Ad

Answer this question