Damage/Reward script not working

My hitbox script won’t ALWAYS damage the NPC. And when it does, the hitbox doesn’t do the specified amount of damage. It “chooses” to deal 15-20 damage to the NPC’s humanoid. To add onto that, when the NPC dies it gives them MORE than the specified amount! Similar to the damage bug. I am thinking of using GetPartsInPart() instead of .Touched, let me know if I should do so. All help is appreciated!

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local db = false
local plrs = game:GetService("Players")
local plr = plrs:GetPlayers()[1] or plrs.PlayerAdded:Wait()

script.Parent.HitBoxEvent.OnServerEvent:Connect(function()
	if db then return end
	db = true

	local Hitbox = Instance.new("Part")
	Hitbox.Parent = script.Parent.Parent
	Hitbox.CFrame = script.Parent.Parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, .5)
	Hitbox.Transparency = .4
	Hitbox.Anchored = false
	Hitbox.Size = Vector3.new(7, 7, 5)
	Hitbox.CanCollide = false

	Hitbox.Touched:Connect(function(hit)
		local npc = hit.Parent:FindFirstChild("NPC")
		local humanoid = hit.Parent:FindFirstChild("Humanoid")

		if npc and humanoid then
			humanoid:TakeDamage(1)

			if humanoid.Health <= 0 then
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats and leaderstats:FindFirstChild("Coins") then
					leaderstats.Coins.Value = leaderstats.Coins.Value + 1
				end
			end
		end
	end)
	game.Debris:AddItem(Hitbox, 0.1)

	wait(0.3)
	db = false
end)
1 Like

On the line before Hitbox.Touched:Connect(function(hit), you can add local touched = false.
On the if npc and humanoid then statement, replace it with
if npc and humanoid and not touched then.
After that line, insert touched = true.
This way it can only damage a humanoid once per hitbox.

2 Likes

You should use PartsInPart because .Touched isn’t always accurate

2 Likes

Makes it do only 1 damage and only give 1 coin now! Thanks!

1 Like

Thanks for the suggestion, I’ll consider adding it!

1 Like

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