I’m a beginner scripter, and I’m having problems with my game.
I created a gun game, simple weapons, apparently nothing too complex, but their damage is accumulating
this is the script for the first gun:
script.Parent.Shot.OnServerEvent:Connect(function(plr, target)
if target:IsDescendantOf(plr.Character) then
print("Not damaging because is player that shot.")
else
if target.Parent:FindFirstChild("Humanoid") then
if target.Name == "Head" then
target.Parent.Humanoid.Health -= 20
print(target.parent,"lost 20 health as it was a headshot.")
else
target.Parent.Humanoid.Health -= 10
print(target.parent, "lost 10 health.")
end
end
end
end)
this is the script for the second gun:
script.Parent.Shot.OnServerEvent:Connect(function(plr, target)
if target:IsDescendantOf(plr.Character) then
print("Not damaging because is player that shot.")
else
if target.Parent:FindFirstChild("Humanoid") then
if target.Name == "Head" then
target.Parent.Humanoid.Health -= 50
print(target.parent,"lost 50 health as it was a headshot.")
else
target.Parent.Humanoid.Health -= 25
print(target.parent, "lost 25 health.")
end
end
end
end)
After I shoot once with each weapon, the damage from the first weapon fired is counted, so when I shoot with the second gun it does its own damage plus that of the first gun, and when I shoot again with the first weapon it records the damage of both.
What happens if you disconnect the connection once the tool is unequipped? Connecting an event returns a RBXScriptConnection which you can store and disconnect later. Try adding local triggerConnection = before connecting the mouse button1 event, and yourToolInstance.Unequipped:Once(function() triggerConnection:Disconnect() end)
I think that you need to disconnect the function that manages the mouse click when the tool is unequipped to solve this problem, like this :
local tool = script.Parent.Parent -- the tool path
local mouse = game.Players.LocalPlayer:GetMouse()
local connection
tool.Equipped:Connect(function()
connection = mouse.Button1Down:Connect(function()
-- your code
end)
end)
tool.Unequipped:Connect(function()
if connection then
connection:Disconnect()
end
end)
@4rdr4ke is correct. You’re establishing a connection when the localscript runs which occurs when you equip the weapon. That connection is never disconnected, so it will always fire the remote for any tool once you’ve equipped it (You should mark his post as the solution.)
^ This is wrong. If they looked closer they’d see that you already have two different guns each with their own Script, Localscript, and RemoteEvent. If this were the correct reasoning, your output would fire both remotes each time, from the start. But when i look at the output you posted, its clear that the first time it only fires one event, and then starts to fire both events once you have equipped the other tool.
Additionally, here’s some supplementary links / info to help you build your gun system:
Tool.Equipped – Will let you reference a players Mouse as the sole argument, and you can write your Mouse.Button1Down connection within the scope of that event.
Tool.Activated – Fires the function you connect to the event when a player does a MouseClick while having the tool activated.
ContextActionService – A service that is designed to let developers bind specific actions when certain conditions are met. It’s probably more than you need at the beginner level, but it’s good to be aware of these services from the start as it lets you optimize your services.