Help with my damage script

‘for some reason the player isn’t being damaged. If anyone has a fix, plz help!’


I need help with my script for a part that damages the player over time dependent on their current speed.
Here’s the script I cooked up

local damagePart = script.Parent
local baseDamage = 10
local speedMultiplier = 0.5
local playersTouching = {}

local function calculateDamage(humanoid)
	local currentSpeed = humanoid.WalkSpeed
	local extraDamage = (currentSpeed - humanoid.WalkSpeed) * speedMultiplier
	return baseDamage + extraDamage
end

local function damageOverTime(humanoid)
	while playersTouching[humanoid] do
		local damage = calculateDamage(humanoid)
		humanoid:TakeDamage(damage)
		wait(1)
	end
end

local function onTouch(hit)
	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		local humanoid = character.Humanoid

		if not playersTouching[humanoid] then
			playersTouching[humanoid] = true
			damageOverTime(humanoid)
		end
	end
end

local function onTouchEnd(hit)
	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		local humanoid = character.Humanoid
		playersTouching[humanoid] = nil
	end
end

damagePart.CanCollide = false

damagePart.Touched:Connect(onTouch)
damagePart.TouchEnded:Connect(onTouchEnd)

The problem is that the part is ‘CanCollide = false’ so for some reason the player isn’t being damaged. If anyone has a fix, plz help!

1 Like

is can-touch disabled or enabled?

It should work, as long as CanTouch is enabled. You should make sure of that.

1 Like

These will cancel out and be 0 then you multiply the multiplier by 0 which gives you 0. You’re basically not damaging the humanoid because you’re taking away 0 hp. You need need to use the HRP’s assembly velocity for the current speed

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.