Problems with a jump script

Hello community, I wanted to inform you about a problem of a script I have and I do not know what the error is, I need your help to change or modify it. What I plan to try is that this is a Jump script (When the person jumps automatically the jump is added to the leaderstats “Jumps”, ), But the problem I have is that not all players on the server are added the jump but the first player who enters the server is added, I want everyone when they jump is added to the leaderstats. What can I do?

local Player = game.Players.PlayerAdded:Wait()

local Character = Player.CharacterAdded:Wait()

local Humanoid = Character:FindFirstChild('Humanoid')

local JumpLeaderstatName = 'Jumps' --leaderstat name to change value when jumping

Humanoid.Jumping:Connect(function(active)

	Player:WaitForChild('leaderstats'):WaitForChild(JumpLeaderstatName).Value += 1

end)

Do you make the leaderstats in another script? Also is this a local or normal script.

This script is in a normal script the same with the leaderstats

Simply because this (seems to be) a server script, which will run only once as soon as a player joins. Since the joining player is the only player, he will obviously be chosen by game.Players.PlayerAdded:Wait(). The rest of the script will only work with this player, it doesn’t account for other players joining.

To make it work, just listen for whenever a player joins, when he does, connect the Jumping event. In addition, waiting for the player and the character is not necessary when working in the server. Waiting for the leaderstats is obliged.

game.Players.PlayerAdded:Connect(function(plr)
    plr:WaitForChild("leaderstats"):WaitForChild("JumpLeaderstatsName").Value += 1 
end)

it’s good to mention that in some cases your initial script will not work for the first player but the second one, there is a chance somehow the player that started the server was fully in before the script waiting for the PlayerAdded event (game.Players.PlayerAdded:Wait()), thus the event waits infinitely until another player joins, which is the second one. Not sure if there is a guarantee that this will not happen, I’d appreciate a correction by someone.

Where do I put this event? “Jumping”

I think I’ve read some posts regarding this elsewhere; it seems to be that even a single yield by the server before connecting the event can cause the first player to be excluded. I’ve done some testing and I found that this was the behaviour, but I haven’t done it at a large scale (maybe if I made something worth playing this wouldn’t be an issue lol).

This can be deceptive because some yielding functions like datastore requests, MessageService stuff, and some others are silently yielding and it’s easy to forget that they could be responsible for missing a player joined on a new server.

1 Like

Damn, then this is something one would need to account for. These loading orders have always scared me!

1 Like

it’s good to mention that in some cases your initial script will not work for the first player but the second one, there is a chance somehow the player that started the server was fully in before the script waiting for the PlayerAdded event ( game.Players.PlayerAdded:Wait() ), thus the event waits infinitely until another player joins, which is the second one. Not sure if there is a guarantee that this will not happen, I’d appreciate a correction by someone.

This can be prevented easily by just looping through the current players in the game. The for loop will begin (if there are players in the game currently). It’s better to have a function instead of doing the same thing in a loop but this is just an example down below.

game.Players.PlayerAdded:Connect(function(plr)
    plr:WaitForChild("leaderstats"):WaitForChild("JumpLeaderstatsName").Value += 1 
end)

-- check for current players
for _, player in ipairs(game.Players:GetPlayers()) do
plr:WaitForChild("leaderstats"):WaitForChild("JumpLeaderstatsName").Value += 1 
end