Creating a Tool Script for Multiple Tools

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)
3 Likes

Where are these scripts located?

3 Likes

Server Script → ServerScriptService
Local Script → StarterGui

Do you have any solutions or ideas as to what the issue may be with the script?

1 Like
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Event = script.Event
local Int = script.Int
local Tool = script.parent
local NewIntValue
Tool.Activated:Connect(function()
NewIntValue = Int.Value - 1
Event:FireServer(NewIntValue)
end)
-- ServerScript
local Event = script.Parent.LocalScript.Event
Event.OnServerEvent:Connect(function(Player, IntValue)
script.Parent.Int.Value = IntValue
end)

So, basically, the local script and the server script are inside the tool. But honestly I’m confused on why you need a local script either way. You can change all these things server sided.

If you really want to still on a script that can be used for multiple tools, look at module scripts.
https://developer.roblox.com/en-us/api-reference/class/ModuleScript

Thank you for your response. I will consider using module scripts.

I still have a question. What causes the remote event to fire 3 times simultaneously?