Adding to value doesn't work

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…

it has to be a server script not a localscript

Nope, that doesn’t work, and besides, this changes player values so it has to be in a localScript… i think

LocalScripts can only run on:

  • A Player’s Backpack , such as a child of a Tool
  • A Player’s character model
  • A Player’s PlayerGui
  • A Player’s PlayerScripts .
  • The ReplicatedFirst service

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
1 Like

There should however be checks for whether

hit.Parent = character

or it might error if the hit isn’t a child of the character at all.

if debounce == false and player then

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)