I have been trying to solve this issue on my own for a few days, and I am still stuck with this problem. I would appreciate any help.
I have a tool that has an IntValue(value set to 20) named, Integer, inside it. I am trying to make it so every time a player clicks their screen when a tool is equipped, it subtracts 1 from the Integer value inside the tool. I am planning to make it so the tools will be transferable between players, so the value must be updated on the server side. In addition, as mentioned in the title of the forum, I am trying to create this system that would only require a local script and a server script. I have a glitch that occurs when I have multiple duplicates of the tool mentioned previously in my backpack. For example, if I have 3 tools named, “Tool 1”, “Tool 2”, and “Tool 3”, which are all the same, but with different names, I get the following output(a part of the output) after clicking a few times for each tool:
-- OUTPUT --
16:51:16.992 Tool 2: Integer Value: 4 - Server - Script:8
16:51:16.992 Tool 2: Integer Value: 3 - Server - Script:8
16:51:16.993 Tool 2: Integer Value: 2 - Server - Script:8
Here are the scripts that are used to produce the output above.
-- LOCAL SCRIPT --
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local backpack = player:WaitForChild("Backpack")
local character = player.Character or player.CharacterAdded:Wait()
local update_tool_event = ReplicatedStorage:WaitForChild("Update_Tool_Event")
backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
child.Activated:Connect(function()
local integer = child.Integer
if integer.Value >= 0 then
update_tool_event:FireServer(child)
end
end)
end
end)
-- SERVER SCRIPT --
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local update_tool_event = ReplicatedStorage:WaitForChild("Update_Tool_Event")
update_tool_event.OnServerEvent:Connect(function(player, tool)
local integer = tool.Integer
if integer.Value > 0 then
integer.Value += -1
print(tool.Name .. ": Integer Value: " .. integer.Value)
elseif integer.Value == 0 then
tool:Destroy()
print("Tool destroyed.")
end
end)