Hi, so I’m making a clicker style game where you click your tool to gain points and progress. But I’ve encountered an issue I can’t seem to solve.
The Issue
The issue is related to the tool. I made a points giver on top of the tool for testing purposes. But, the points giver seems to overwrite the tool’s stats in the leaderstats.
Code for tool:
local tool = script.Parent
local anim = tool:WaitForChild('Animation')
local player = game:GetService("Players").LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local loadanim = humanoid:LoadAnimation(anim)
local canhit = true
tool.Activated:Connect(function()
if canhit == true then
canhit = false
loadanim:Play()
local points = player:WaitForChild("leaderstats"):WaitForChild("Strength")
points.Value = points.Value +1
task.wait(0.8)
canhit = true
end
end)
vs. The points giver part
script.Parent.ClickDetector.MouseClick:Connect(function(Player)
local leaderstats = Player:WaitForChild("leaderstats")
local Points = leaderstats:WaitForChild("Strength")
Points.Value += 1
end)
On top of this, the sell part (selling the points for money) will only work with the point giver’s stats, not the tool’s. It’s almost like the tool is giving a ghost value or something. Any insight would be appreciated, thanks.
A few things
In your tool, you’re setting the points via points.Value = cash.Value + 1, shouldn’t it be points.Value += 1?
Also the issue is most likely because your tool is giving points locally whereas your mouse click giver is server sided, converting your tool script to a regular script s hould work
Doesn’t require much changing either, you get the player via detecting when the tool is equipped, when it equips it’s put in the character, so you get the character from there and use Players:GetPlayerFromCharacter() to get the player and from there you can get the leaderstats
Thanks, so where would I put the Players:GetPlayerFromCharacter() in the script then?
It’s not just a matter of putting that function in, your tool script needs to be recoded to work on the server, which requires using the Equipped and probably the Unequipped event for the tool to get the character which is then used to get the player
Makes sense, thank you. I’m not sure how to do that though, is there any tutorials or devforum posts you know that I could look at?
That I’m not sure exactly, there might be, though doing research on how the events I had mentioned and how the function I wrote worked should be enough. When you equip the tool, it’s placed in your character, so you go backwards with enough .Parents to get the character, then with that character, use the function to get the player, so then when you click, it gives points to the player
1 Like
local tool = script.Parent
local function onEquipped(_mouse)
local Players = game:GetService("Players")
local char = workspace.Player
local player = Players:GetPlayerFromCharacter(char)
local leaderstats = player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
tool.Activated:Connect(function()
strength.Value += 1
end)
end
tool.Equipped:Connect(onEquipped)
This is what I’ve made, but it doesn’t work
Alright so, a few things
_mouse is not needed here so you can remove it,
You can put the Players variable outside of the function since it’s not really needed there
char has to be the Character model, which you get most likely by doing tool:FindFirstAncestorOfClass("Model"), or enough .Parents to get the character
I wouldn’t recommend putting the activated event inside of the equipped function as then everytime you equip it’ll make the event again and cause you to gain more points, best to put it outside and then inside of the Activated event you grab the strength value and increment it
local tool = script.Parent
local Players = game:GetService("Players")
local char = tool:FindFirstAncestorOfClass("Model")
local player = Players:GetPlayerFromCharacter(char)
local function onEquipped()
end
tool.Activated:Connect(function()
local leaderstats = player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
strength.Value += 1
end)
tool.Equipped:Connect(onEquipped)
I don’t think I did it right. Do I even need the onEquipped function? I don’t really need it at the moment, but eventually I would want an idle animation for holding the tool
char and player both need to be in the onEquipped as we need to get the character when they equip the tool, the idle animation you also do it when the equip it
Alright I did that, but the leaderstats still aren’t changing
local tool = script.Parent
local Players = game:GetService("Players")
local function onEquipped()
local char = tool:FindFirstAncestorOfClass("Model")
local player = Players:GetPlayerFromCharacter(char)
end
tool.Activated:Connect(function(Player)
local leaderstats = Player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
strength.Value += 1
end)
tool.Equipped:Connect(onEquipped)
ANy error showing up? The only issue I can see is that you assume the thing that is passed when you activate is the player, which it isn’t, remove Player and replace it with player
local tool = script.Parent
local Players = game:GetService("Players")
local function onEquipped()
local char = tool:FindFirstAncestorOfClass("Model")
local player = Players:GetPlayerFromCharacter(char)
end
tool.Activated:Connect(function()
local leaderstats = player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
strength.Value += 1
end)
tool.Equipped:Connect(onEquipped)
I tried that, still not working, I have no clue why?
local tool = script.Parent
local Players = game:GetService("Players")
local function onEquipped()
local char = tool:FindFirstAncestorOfClass("Model")
local player = Players:GetPlayerFromCharacter(char)
end
tool.Activated:Connect(function(player)
local leaderstats = player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
print("success")
strength.Value += 1
end)
tool.Equipped:Connect(onEquipped)
I added a print statement, and have deduced that it’s not anything wrong with the leaderstats, but the script itself.
Try removing player from here? It might be overwriting the player we found
Oh and also, you should have a local player outside of the function as well because so far player is local to the equipped function
local tool = script.Parent
local Players = game:GetService("Players")
local player
local function onEquipped()
local char = tool:FindFirstAncestorOfClass("Model")
player = Players:GetPlayerFromCharacter(char)
end
tool.Activated:Connect(function()
local leaderstats = player:WaitForChild("leaderstats")
local strength = leaderstats:WaitForChild("Strength")
print("success")
strength.Value += 1
end)
tool.Equipped:Connect(onEquipped)
If the local player is moved outside the function then it also needs the local char, unless I can remove that?
You can, though I only put the local player out because you have no use for the character besides to get the player when you equip the tool, whereas you have a use for the player when you activate the tool
I have an error: Argument 1 missing or nil
It just points to the local player
local player = Players:GetPlayerFromCharacter()
Odd, FindFirstAncestorOfClass should get the character, can you try printing it? If it returns nil, you probably need to use enough .Parents to get the character from there, most likely tool.Parent can get you there unless your tool is more complicated
Unless you forgot to put char in the brackets, in which case you should put it
local function onEquipped()
local char = tool:FindFirstAncestorOfClass("Model")
print("success")
end
It didn’t print anything when I equipped the tool, so yeah it’s probably an issue with the FindFirstAncestorOfClass
I’m not entirely sure what .Parents to use then. My tool has the handle part and the script, then the actual tool, then it’s in starterpack. Also, what do you mean by char in brackets?
The amount of .Parent you should use depends on how many backwards movements it would take to get to the Character. Your script is the child of the tool from how script.Parent leads to the tool itself, so in that case you can do local char = tool.Parent instead
player = Players:GetPlayerFromCharacter(char), in the code you placed when you get an error, you didn’t put char in the brackets, so I wasn’t sure if you had forgot to do that or not