Hi everyone! I have a script that adds 5 Points to a player’s points value, but it doesn’t work. This is a LocalScript located in the part in itself. The Points value is a part of leaderstats and that part is invisible.
here is my code:
local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local debounce = false
print("hi")
script.Parent.Touched:Connect(function()
print("Touched")
if debounce == false then
print("Debounce false")
debounce = true
plr.leaderstats.Points.Value = plr.leaderstats.Points.Value + 5
print("added points")
wait(120)
debounce = false
else
end
end)
The print statements were used for debugging although NONE of them printed, which probably means the entire script isn’t working…
As a general rule, you never want to change values from a LocalScript for security reasons. Either way, since you aren’t using a RemoteEvent the changes will not be replicated to the server.
Consider doing this in a ServerScript using the hit argument of the Touched event:
local debounce = false
print("hi")
script.Parent.Touched:Connect(function(hit) --hit=part that hit script.Parent.
local player = game.Players:GetPlayerFromCharacter(hit.Parent) --Will either return the player, or nil
print("Touched")
if debounce == false and player then
print("Debounce false")
debounce = true
player.leaderstats.Points.Value = player.leaderstats.Points.Value + 5
print("added points")
wait(120)
debounce = false
else
end
end
That line ensures hit.Parent = character. Had hit.Parent != character, the player variable would be nil, and the else statement would run. (The :GetPlayerFromCharacter function either returns the player, or nil)