Hitbox with .Touched deals more damage than it should

I’ve used .Touched with knowing it’s not good, but the thing is even if I tried to fix that so player can’t touch it anymore, it still touches. What’s the problem?

local Hit = {}
script.Parent.Touched:Connect(function(hit)
	local chr = hit.Parent
	local hum = chr:FindFirstChild("Humanoid")
	if chr and hum and chr.Name ~= script.PlayerWhoUsed.Value and hum.Health >0 then
		if chr:FindFirstChildOfClass("ForceField") then return end
		if Hit[chr.Name] == true then return end
		
		if Hit[chr.Name] == nil then
			Hit[chr.Name] = true
		end
--checker so cant touch anymore(PROBLEM NO WORK...)
		script.Parent.BrickColor = BrickColor.Green()
		hum.Health = hum.Health - math.random(1,3) --deals damage
--actually could use :TakeDamage idk why Iused hum.Health = hum.Health
		Hit[chr.Name] = false
	end
end)

You’re strictly looking for nil then you set it to true, afterwards, it is set to false so you nullify what you were trying to do.

I made a script that should solve what you are trying to accomplish and is more simple:

local Hit = {}
script.Parent.Touched:Connect(function(hit)
	local parent = hit.Parent -- Parent will exist, there is no physical object in the world that you can touch without a parent.
	local hum = parent:FindFirstChild("Humanoid")
	if hum and hum.Health ~= 0 and parent.Name ~= script.PlayerWhoUsed.Value then
		if not table.find(Hit, parent) then -- Instances can be used as indexes, this lets you have NPCs with the same name unless this is strictly for players only, if so, then just put it to parent.Name
			script.Parent.BrickColor = BrickColor.Green()
			
			local randomDmg = Random.new():NextInteger(1,3) -- same as hum.Health - math.random(1, 3)
			
			hum:TakeDamage( math.max(randomDmg, 0) ) -- ensures the damage isn't negative but I don't know if this'd be a problem
			
			table.insert(Hit, parent)
		end
	end
end)

(clicked once)

image

I edited my code and fixed the mistake. I made it take damage from humanoid’s health minus the random dmg which caused a huge loss in health. It should be fixed now.

1 Like

yeah fixed now, thanks!

30