Weapon Swing / Hitbox Script

This script is supposed to hurt player when the weapon is swung but after swinging the weapon is still able to kill so I tried using connection / disconnect() but now it doesn’t work at all. Could someone please help me?

-- Server Script
local remoteEvent = game.ReplicatedStorage.DamageRM
local coolDown = true
local connection

connection = remoteEvent.OnServerEvent:Connect(function(plr, hit)
		hit.Parent.Humanoid.Health = 0
end)
task.wait(2)
connection:Disconnect()

-- Local Script
local tool = script.Parent.Parent
local myPillowModel = game.Players.LocalPlayer.Character.PillowModel
local damageRM = game.ReplicatedStorage:WaitForChild("DamageRM")
local canDmg = true

local soundService = game:GetService("SoundService")

local humanoid = game.Players.LocalPlayer.Character.Humanoid

--IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
local HitAnimation = Instance.new("Animation")
HitAnimation.AnimationId = "rbxassetid://15910080700"

local HitAnimationTrack = humanoid:LoadAnimation(HitAnimation)

-- IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII
local toolRunAnimation = Instance.new("Animation")
toolRunAnimation.AnimationId = "rbxassetid://15910039503"

local toolAnimationTrack = humanoid:LoadAnimation(toolRunAnimation)

local sound = Instance.new("Sound", tool)
sound.SoundId = "rbxassetid://9120386436" 

tool.Equipped:Connect(function()
	myPillowModel.Transparency = 1
	toolAnimationTrack:Play()
end)

tool.Unequipped:Connect(function()
	myPillowModel.Transparency = 0
	toolAnimationTrack:Stop()
end)

tool.Activated:Connect(function()
	if canDmg then
		canDmg = false
		toolAnimationTrack:Stop()
		HitAnimationTrack:Play()
		tool.Handle.Hitbox.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid")then
				damageRM:FireServer(hit)
			end
		
		end)
		task.wait(1)
		toolAnimationTrack:Play()
		task.wait(2)
		canDmg = true
		else
			soundService:PlayLocalSound(sound)
	end
		

	
end)
1 Like

Every time you click, a new touched function is created. The server script never re-creates the onServerEvent, so after it fires once and gets deleted, its gone forever. Also, having a local script just firing to the server leaves it open for MANY exploits. I would disconnect on the local side instead, and also add some verification to the server part, like a debounce and possibly add a way to make sure they arent trying to damage something across the map.

That is the problem here, I would recommend using a module like RaycastHitbox because of the unreliable Touched event and trusting the client too much.

I fixed it by copying the tool.Activate function into the server script then leaving all the parts that play the animation and sound (excluding the damaging player) in a client script

I literally forgot about that module… I’ll try using that to see how it works but so far the touched event I have now works.