Tool not adding stats value

  • So i’m trying to make it that when this tool is activated, a leaderstats value is increased, but it will not work

  • here is the code:

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local BGS = Player:WaitForChild("BackgroundStats")

local Equipped = false

script.Parent.Equipped:Connect(function(mouse)
	Equipped = true

	mouse.Button1Down:Connect(function()
		if Equipped then
			local Strength = BGS:WaitForChild("Strength")
			print("Add strength")
			Strength.Value = Strength.Value + 1
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	Equipped = false
end)

  • is there something i am missing out?

Leaderstats value in the Client-Side is little broken.
You can make your script in the Server Side.
Use Script and not Local Script.

I have a question: Why you add a leaderstats variable but you didn’t use?

K i’ll try that, i added leaderstats for just incase i’d use it later, i kinda don’t know how to change it to the server side

So, you can continue using a local script, but,

instead of Backgroundstats just use normal leaderstats, use UserInputService, and declare a variable to keep track if the mouse is being pressed.

local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer

local isPressed = false

script.Parent.Equipped:Connect(function()
	uis.InputBegan:Connect(function(inp)
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then
			isPressed = true
			
			while isPressed == true do
				player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
			end
		end
	end)
end)

script.Parent.Unequipped:Connect(function()
	uis.InputBegan:Connect(function(inp)
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then
			isPressed = false
		end
	end)
end)

You can not use a regular script in a tool, when talking about leaderstats. When a firing a function there’s no parameter to get the player. So the only way to code with the leaderstats using a tool is by a LocalScript.

it doesn’t seem to work (and yes i changed all the values to leaderstats)

Have you declared the leaderstats script in ServerScriptService?

yes i have declared the leaderstats script

Finally i have fixed my script.

Note: The player leaderstats value is incrementing while the mouse button is being held if you want to player to earn strength by only clicking just remove the the → while isPressed == true loop; but the keep the player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1.

Hope it helped!

Edit: I forgot to mention that because all of this is made in a LocalScript the only one who will see changes will be the client itself. And so I added a Remote Event named as shown, and fired the server whenever the player is holding the mouse. And all the stats changes now will be seen by all the players in the server.

local uis = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer

local isEquipped = false
local isPressed = false

script.Parent.Equipped:Connect(function()
	isEquipped =true
end)

script.Parent.Unequipped:Connect(function()
	isEquipped = false
end)

uis.InputBegan:Connect(function(inp)
	if isEquipped then
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then
			isPressed = true
			
			while isPressed == true do
				task.wait(0.1) -- Change it to whatever you want
				game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent"):FireServer()
			end
		end
	end
end)

uis.InputEnded:Connect(function(inp)
	if isEquipped then
		if inp.UserInputType == Enum.UserInputType.MouseButton1 then
			isPressed = false
		end
	end
end)

Second script(put it in the ServerScriptService):

game:GetService("ReplicatedStorage"):WaitForChild("RemoteEvent").OnServerEvent:Connect(function(player)
	player.leaderstats.Strength.Value = player.leaderstats.Strength.Value + 1
end)
2 Likes

It doesn’t work but it seems it should, i put print statements for when it gets equipped and unequipped and it won’t print, maybe it’s a bug in my studio?

thanks so much, i just realised the reason it never worked was because i had “requires handle” on and it never had a handle

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.