Hey, I am making a sword, and it works perfectly fine. But the only problem is that when i activate the sword, tool, it does 25 damage but it damages the players multiple times even though there’s a debounce, how can I fix it?
Thanks!
Server script:
-- Variables
local Tool = script.Parent.Parent --Finds Tool
local Handle = Tool:WaitForChild("Handle") --Waits for child "Handle"
local Model = Tool:WaitForChild("Model") --Waits for child "Model"
local Slash = script:WaitForChild("Slash") --Waits for child "Slash"
local Debounce = false --I don't know this one
local PlayersHit = {} --When player hits someone
--Adds Weld
--Weld is something you add in a tool that will not break
for i, Parts in pairs(Model:GetChildren()) do --To get every child in Tools
if Parts:IsA("BasePart") then --Check if children models is BasePart
local Weld = Instance.new("WeldConstraint") --Created new Variable called "Weld"
Weld.Part0 = Parts --Welds parts
Weld.Part1 = Handle --Welds Handle
Weld.Parent = Parts --Welds parts
end
end
--Animate
Tool.Activated:Connect(function() --Activates event
if Debounce == false then
Debounce = true --If Debounce is false then it will change to true
local Humanoid = Tool.Parent:WaitForChild("Humanoid") --Making Variable to "Humanoid", would be character if it's equipped
local AnimTrack = Humanoid:LoadAnimation(Slash) --When clicked played animation
AnimTrack:Play() --Play function for AnimTrack
wait(1) --Wait 1 second
Debounce = false --Activate to false after Animation played
end
end)
--Damage
Handle.Touched:Connect(function(Hit) --When player clicked
if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then
if Debounce == true and PlayersHit[Hit.Parent] == nil then
Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(25) --You can change to whatever damage you want
PlayersHit[Hit.Parent] = true
wait(1)
PlayersHit[Hit.Parent] = nil
end
end
end)
Handle.Touched:Connect(function(Hit) --When player clicked
if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then
if Debounce == true and PlayersHit[Hit.Parent] == nil then
Hit.Parent:FindFirstChild("Humanoid"):TakeDamage(25) --You can change to whatever damage you want
PlayersHit[Hit.Parent] = true
end
end
end)
and for the activated part
Tool.Activated:Connect(function() --Activates event
if Debounce == false then
PlayersHit = {} --Empty hit players to hit them again
Debounce = true --If Debounce is false then it will change to true
local Humanoid = Tool.Parent:WaitForChild("Humanoid") --Making Variable to "Humanoid", would be character if it's equipped
local AnimTrack = Humanoid:LoadAnimation(Slash) --When clicked played animation
AnimTrack:Play() --Play function for AnimTrack
wait(1) --Wait 1 second
Debounce = false --Activate to false after Animation played
end
end)
this might not work as this is the simplest possible fix. Try this and if it doesnt work I can try to come with a better solution.
can you provide more context on what the problem is, as the code you normally had and the one I provided should work as you desired. I think I didnt understand your problem correctly?
So like, when u activate the tool, which is the sword, it is supposed to deal 25 damage every 1 second, but the problem is that it it damages the player multiple times and doesn’t have a debounce even though it’s in the code, so it deals multiple 25 damage until 1 second is over, then it stops. I am not sure how to fix it as the code seems understandable and be expected to work.
I implemented your damaging thing a little differently to a damaging part
and this one worked just like you desired only damages once until the debounce is reset to true
I used a killpart as its easier to implement
local part = script.Parent
local hitplrs = {}
local candamage = false --same as debounce in your script
part.ClickDetector.MouseClick:Connect(function()
hitplrs = {}
candamage = not candamage
--you can ignore this, this is for testing
part.Color = candamage and Color3.new(1, 0.0117647, 0.0627451) or Color3.fromRGB(47, 165, 5)
end)
part.Touched:Connect(function(h)
if not candamage then
return
end
local hit = h.Parent
if hit:FindFirstChild("Humanoid") then
if hitplrs[hit] then
return
end
hitplrs[hit] = true
hit.Humanoid:TakeDamage(25)
end
end)
this might give you the idea
still very weird why your code doesnt work though
Are you sure you don’t have any duplicate scripts that are present/being cloned? I pretty much copy pasted your code in a new sword and it works as normal.