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

How do I fix script not printing in a PlayerAdded function?

Asked by 5 years ago

There isn't any errors when I run the game but this doesn't print when I put my XP value to 100:

game.Players.PlayerAdded:Connect(function(plr)

local Xp = plr.XP

if Xp.Value == 100 then

print("Hey I have 100 xp ")

end

end)

1 answer

Log in to vote
0
Answered by
awfulszn 394 Moderation Voter
5 years ago

Your issue here is that you're checking if the value of the player's XP is equal to 100 as soon as they join the game, not once you actually change it to 100.

We can fix this by using .Changed on the value.

Essentially .Changed is just an event that fires once a value is changed.

Note: This would be used in a LocalScript located somewhere inside the player, such as StarterGui.

local plr = game.Players.LocalPlayer
local Xp = plr:WaitForChild("XP") -- Using WaitForChild() so the script won't break if "XP" has not loaded yet.

Xp.Changed:Connect(function()
    if Xp.Value == 100 then
        print("Hey I have 100 XP!")
    end
end)

If you have any comments or queries, then just leave a comment here! Learn more about the .Changed event here.

Happy scripting!

Ad

Answer this question