Please help with damaging script

I’m trying to make the damage script inside of the hitbox part be enabled when you swing and then disable when the swing is done. The script is enabling and disabling at the right times but it doesn’t do damage at all.
Here’s my script that makes it enabled and disable when I need it to:

-----------------------Swing1--------------------------------
mouse.Button1Down:Connect(function()
	if Turn == 1 and Sheathed1.Transparency == 1 and Unsheathed1.Transparency == 0 then
		ChrSwing1:Play()
		Swing1S:Play()
		Damage.Enabled = true --damaging starts
		wait(0.5)
		Damage.Enabled = false --damaging stops
		ChrSwing1.Stopped:wait(cooldown)
		Turn = 2

-----------------------Swing2--------------------------------
	elseif Turn == 2 and Sheathed1.Transparency == 1 and Unsheathed1.Transparency == 0 then
		ChrSwing2:Play()
		Swing2S:Play()
		Damage.Enabled = true --damaging starts
		wait(0.8)
		Damage.Enabled = false --damaging stops
		ChrSwing2.Stopped:wait(cooldown)
			Turn = 1
	else
		return
	end
end)

and here’s the damaging script:

function onTouched(part)
	local h = part.Parent:findFirstChild("Humanoid")
	if h~=nil then
		h.Health = h.Health -100
	end
end

script.Parent.Touched:connect(onTouched)
1 Like

If the top script is a localscript, and the bottom script is a server script, that’s good, but you just need another way to tell the server when damage should start/stop.

Changes to values don’t replicate from client to server. Use a remoteevent.

I know this is irrelevant but you should use h.Health -= 100 instead of h.Health = h.Health -100

I think it’s best to use Humanoid:TakeDamage(100) because it makes the border of the screen flash red like the blood effect I think. It also takes ForceFields into consideration.

1 Like

Hello, I think I found a problem with your scripts. The problem could be related to the disabling and enabling of the Damage script. If a script is disabled, the connections it established (like Touched events) won’t be reestablished when you enable it again.

local canDamage = false

function onTouched(part)
	if canDamage then
		local h = part.Parent:findFirstChild("Humanoid")
		if h ~= nil then
			h.Health = h.Health - 100
		end
	end
end

script.Parent.Touched:connect(onTouched)

-- Expose these functions to be called from other scripts:
script.SetCanDamage = function(value)
	canDamage = value
end

mouse.Button1Down:Connect(function()
	if Turn == 1 and Sheathed1.Transparency == 1 and Unsheathed1.Transparency == 0 then
		ChrSwing1:Play()
		Swing1S:Play()
		Damage.SetCanDamage(true) --damaging starts
		wait(0.5)
		Damage.SetCanDamage(false) --damaging stops
		ChrSwing1.Stopped:wait(cooldown)
		Turn = 2

	elseif Turn == 2 and Sheathed1.Transparency == 1 and Unsheathed1.Transparency == 0 then
		ChrSwing2:Play()
		Swing2S:Play()
		Damage.SetCanDamage(true) --damaging starts
		wait(0.8)
		Damage.SetCanDamage(false) --damaging stops
		ChrSwing2.Stopped:wait(cooldown)
		Turn = 1
	else
		return
	end
end)

This should fix your problem!