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)
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.
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)
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?