Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I have an npc that shoots at the player but it damages more than expected. Why??

So i have this npc that shoots at the player but it damages more than expected example: when it shoots it hits the player and then it takes more than the expected damage. I have tried using debounce but its not working. The code part is from line 84 to 99

local players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local bullet = ServerStorage.Bullet

local NPC = script.Parent
local humanoid = NPC:WaitForChild("Humanoid")

local gun = script.Parent.Gun
local gunHandle = gun:WaitForChild("Handle")
local shootPart = gun:WaitForChild("ShootPart")

local fireRate = 2.5
local damage = 10
local bulletSpeed = 100

local breakLoop = false

local function OnShoot()
	shootPart.GunFlash.Enabled = true
	shootPart.FlashGui.Enabled = true
	shootPart.Swoosh:Play()

	wait(0.5)
	shootPart.GunFlash.Enabled = false
	shootPart.FlashGui.Enabled = false
end

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)	
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")

		local npcFollowPart = Instance.new("Part")
		local weldConstraint = Instance.new("WeldConstraint", npcFollowPart)

		npcFollowPart.CFrame = humanoidRootPart.CFrame * CFrame.new(0, 0, 7)
		npcFollowPart.CanCollide = false
		npcFollowPart.Transparency = 1

		weldConstraint.Part0 = npcFollowPart
		weldConstraint.Part1 = humanoidRootPart

		npcFollowPart.Size = Vector3.new(1,1,1)
		npcFollowPart.Parent = character
		npcFollowPart.Name = "FollowPart"

		local HoldAnim = NPC:WaitForChild("Hold")

		local controller = NPC.Humanoid
		controller:LoadAnimation(HoldAnim):Play()

		while wait() do			
			if breakLoop == true then
				break
			end

			NPC.Torso.CFrame = CFrame.new(NPC.Torso.Position, character.HumanoidRootPart.Position) 

			NPC.Humanoid:MoveTo(npcFollowPart.Position)

			if humanoid.Health == 0 then
				breakLoop = true

				shootPart:Destroy()				
				NPC.Torso.Oof:Play()
				wait(0.1)
				NPC.HumanoidRootPart:Destroy()
			end
		end		
	end)
end)

while wait(fireRate) do
	local newBullet = bullet:Clone()	
	local ShootAnim = NPC:WaitForChild("Shoot")

	local controller = NPC.Humanoid

	if breakLoop == true then
		break
	end

	controller:LoadAnimation(ShootAnim):Play()

	newBullet.Touched:Connect(function(hit)
		local debounce = false

		local human = hit.Parent:FindFirstChild("Humanoid")

		if human and not debounce then
			debounce = true

			human:TakeDamage(damage)
			shootPart.Swoosh:Stop()
			shootPart.Explosion:Play()

			wait(2.6)
			debounce = false
		end
	end)

	newBullet.Position = shootPart.Position
	newBullet.Parent = game.Workspace		
	newBullet.Velocity = shootPart.CFrame.LookVector * bulletSpeed

	OnShoot()
end

1 answer

✓ Accepted answer

Line 085 should be on line 083.

This discussion is preserved as it was. New answers, comments, and votes are closed.