No, you don’t have to put a while loop, as the code inside the connection is run every time scoreVal is changed. Also, inside your definition for scoreVal, it should be player:WaitForChild(“leaderstats”) in case it hasn’t loaded in yet.
yes i know im just confused as im thinking that it is PlayerAdded which means it only checks if the player joins and not when they are actually in game
because im thinking what the script does is
Example:
Im in game and i have 99 survivals
I play a bit more then i have 100 survivals
I dont get a badge
I leave then rejoin
Then it gives me the badge for having 100 survivals (since survivals save in the game im talking about)
Basically that script, whenever the value of your whatever has changed, for instance kills goes from 9 to 10 it fires, and then checks if the kill value is greater than or equal to 10, if so and the player doesn’t own the badge then it will award the user with the badge!
The PlayerAdded event will only fire once per player, but the scoreVal.Changed connection will fire everytime the scoreVal leaderstats changes.
But im thinking it will only fire once since it occurs in a playeradded function which like you said fire only once
Think of it like a chat connection, it never disconnects.
If you only want it to fire when the player joins the game, remove the changed event.
No that’s not the case, the connection will not fire once because of the playerAdded event don’t worry, the connection will fire everytime the scoreVal leaderstats changes.
The PlayerAdded event will make the connection and that is It’s job, once the connection is made it’ll not be dependant of PlayerAdded anymore.
Yeah, keep in mind this only fires when the value is changed; rather than the player joining ^ as said above you can award the player when they ONLY join by removing the changed event, the change event basically waits for the value to increment or decrease after that it fires and it does the rest below.
Yes, I explained that to him further up in the thread. I’m not sure what leo is asking now.
PlayerAdded gets the player once they have connected to your game. In this regard it only runs once. However the player is stored, so you do not need to worry about it only running once.
To clarify further, this is because you listened to the .Changed event within the PlayerAdded event. The game will continue to listen to that .Changed event, even after the PlayerAdded event has finished running.
this is all really confusing but im gonna try to be as clear as i can
There is a function in a function
The 1st function only occurs when the player is added or joins
The 2nd function checks the score to see if a player has more or equal to a score
the 2nd function is inside the 1st function which only occurs once
I’m confused as to since the 1st function only occurs once then the 2nd will only occur once
its like you are telling me something inside a script becomes its own script
local badgeService = game:GetService("BadgeService")
local badgeId = 0
game.Players.PlayerAdded:Connect(function(player) -- This only happens once
local scoreVal = player:WaitForChild("leaderstats").Score -- Wait for the leaderstats to be added to the player from your other script
scoreVal.Changed:Connect(xpcall(function() -- This function is triggered whenever the value changes
if scoreVal.Value >= 10 and not badgeService:UserOwnsBadgeAsync(player.UserId, badgeId) then
badgeService:AwardBadge(player.UserId, badgeId) -- Give the badge
end
end, warn))
end)
In a way I suppose you could think about it like that. You’re not working with a linear script anymore, since you are using events. Those events will continue to listen for whatever is happening, even after the rest of the script has finished.
Basically, connections don’t really depend on other connections to fire.
The .Changed connection fires everytime the scoreVal leaderstat changes, and it doesn’t matter if PlayerAdded is firing or not.
The .Changed connection will only need the PlayerAdded connection once, then It’ll be completly independent.
I recommend reading more about Handling Events, and the .Changed connection as it may help you.
Exactly my question
Will the changed event continue to fire everytime the players score changes even with the rest of the script finished?
Yes, the .Changed event will continue to trigger everytime the score changes, independent of other connections.
Unless you disconnect it through Disconnect() but that’s NOT the case.
Yes. It will continue to fire whenever the score is changed.
Also one more question, does the function still need to be a pcall, i dont think it needs to since it isn’t part of the main script anymore