I was trying to make a punch tool, but I encountered a problem where the touched event fires repeatedly even WITH a debounce. I used a LocalScript with a RemoteEvent. There are also no errors.
Local Script: (I am having trouble with lines around 32 and 64.)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local tool = script.Parent
local configurationFolder = script.Parent:WaitForChild("Configuration")
local debounce = false
tool.Activated:Connect(function()
if not debounce then
debounce = true
local humanoid = localPlayer.Character:WaitForChild("Humanoid")
if humanoid.RigType == Enum.HumanoidRigType.R6 then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://04875004180"
animation.Parent = humanoid
local animationToLoad = {animation}
ContentProvider:PreloadAsync(animationToLoad)
local loadedAnimation = humanoid:LoadAnimation(animation)
loadedAnimation:Play()
local hitDebounce = false
localPlayer.Character:WaitForChild("Right Arm").Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
if hit.Parent:WaitForChild("Humanoid").Health > 0 and not hitDebounce then
hitDebounce = true
local targetCharacter = hit.Parent
ReplicatedStorage:WaitForChild("KillHumanoidEvent"):FireServer(targetCharacter:WaitForChild("Humanoid"), script.Parent:WaitForChild("punch"), configurationFolder:WaitForChild("Damage").Value)
wait(1)
hitDebounce = false
return nil
end
end
end)
else
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://4875026093"
animation.Parent = humanoid
local animationToLoad = {animation}
ContentProvider:PreloadAsync(animationToLoad)
local loadedAnimation = humanoid:LoadAnimation(animation)
loadedAnimation:Play()
local hitDebounce = false
localPlayer.Character:WaitForChild("RightHand").Touched:Connect(function(hit)
if hit.Parent:FindFirstChildWhichIsA("Humanoid") then
if hit.Parent:WaitForChild("Humanoid").Health > 0 and not hitDebounce then
hitDebounce = true
local targetCharacter = hit.Parent
ReplicatedStorage:WaitForChild("KillHumanoidEvent"):FireServer(targetCharacter:WaitForChild("Humanoid"), script.Parent:WaitForChild("punch"), configurationFolder:WaitForChild("Damage").Value)
wait(1)
hitDebounce = false
return nil
end
end
end)
end
wait(1)
debounce = false
end
end)
Server Script: (It works fine.)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ContentProvider = game:GetService("ContentProvider")
ReplicatedStorage:WaitForChild("KillHumanoidEvent").OnServerEvent:Connect(function(firerPlayer, targetHumanoid, sound, damage)
local soundToLoad = {sound}
ContentProvider:PreloadAsync(soundToLoad)
targetHumanoid:TakeDamage(damage)
sound:Play()
end)

6 comments
Try printing lets see what it doss.
If that doesnt work retype it by scratch.
The first time I punch it, it prints once. The second time I punch it, it prints twice.
At line 2 do Players.PlayerAdded:Connect(function(player) end)
Okay, but how would that help because LocalScript's run on the client, so the script wouldn't run without a player. Plus, LocalScript's only run on a tool when equipped. (As it's a descendant of a player's character.)
I tried it and now it doesn't even work.