How to use a tool to give points to a player when clicking

Hello, I’m learning to program and I encountered a difficulty with what I want to do.
My idea in this script was that with each click using the tool, a point would be added to the leaderstats. The problem is that the script is not working, and as I’m still learning, I haven’t been able to come up with a solution to understand why it’s not working.
Particularly, I believe it might be a problem with the path I’m providing for the script to find the leaderstats, but I’m not sure if that’s the issue.

The script is as follows:


local Tool = script.Parent
local Grips = {
	Idle = CFrame.new(0.5, -0.1, 0, 0, -1, -0, -1, 0, -0, 0, 0, -1),
	Drinking = CFrame.new(1.3, -1, 1, 0, -1, -0, -1, 0, 0, 1, 1.5, -1)
}


local Player = game.Players.LocalPlayer
local Gold = Player:WaitForChild('leaderstats'):WaitForChild('gold')




Tool.Activated:Connect(function()
	
	
	Tool.Grip = Grips.Drinking
	Tool.Handle.Sound:Play()
	
	wait(2)
	
	Tool.Grip = Grips.Idle
	
	Gold.Value = Gold.Value + 10
	
end) 

I don’t know how to work with leaderstats, so I relied on various scripts from the toolbox and took a look at Roblox’s documentation. That’s where the leaderstats came from. :sweat_smile:

Thank you for reading! :+1:

1 Like

The problem is that you’re changing the value on a LocalScript, which won’t affect the server. Change your value on a normal script and it should work.

I’m still very much a beginner. Is this value the script that is added to the tool, or are the values written directly inside this script?

Here is a screenshot of what appears on my screen when I run the code.

Ignore all i said earlier this is not working because ur doing it on a server side script in which you cannot call the local player if u want to refer to the player do this instead:

local Player = game:GetService("Players"):GetPlayerFromCharacter(script.Parent.Parent)

1 Like

If this script is a normal script, then you can not use game.Players.LocalPlayer. That only works in a localscript, which is a script that runs on the player’s device(client).

To get the player, you can use:

game:GetService("Players"):GetPlayerFromCharacter(Tool.Parent)

since the tool while equipped is in the player’s character.

3 Likes

for you script:

you just needed to add the “Tool.Equipped” function then add the “Tool.Activated” function.

the script needs to be ran when the tool is equipped so you need to tell it to run when the tool is equipped otherwise it won’t function.

local Tool = script.Parent
local Grips = {
	Idle = CFrame.new(0.5, -0.1, 0, 0, -1, -0, -1, 0, -0, 0, 0, -1),
	Drinking = CFrame.new(1.3, -1, 1, 0, -1, -0, -1, 0, 0, 1, 1.5, -1)
}


local Player = game.Players.LocalPlayer
local Gold = Player:WaitForChild('leaderstats'):WaitForChild('gold')

Tool.Equipped:Connect(function()
		Tool.Activated:Connect(function()
	
		Tool.Grip = Grips.Drinking
		Tool.Handle.Sound:Play()

		wait(2)

		Tool.Grip = Grips.Idle

		Gold.Value = Gold.Value + 10

	end)
end)