Raycasting Damages and brings NPC health below 0, but doesn't stop moving

I am trying to make a laser gun using Raycasting and it actually works, it shoots and hurts NPCs, but for some reason the NPC continues to move even though its health is below zero, it might be because it’s a local script and not a script but I have tried putting it in a server script and it doesn’t even work, I have also tried looking it up and I haven’t found anything that helps me out or fixes my problem. Any help would be great.
Local script

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local debounce = false
tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if debounce == false then		
			local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Handle.CFrame.p).unit * 300)
			local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

			local beam = Instance.new("Part", workspace)
			beam.BrickColor = BrickColor.new("Maroon")
			beam.FormFactor = "Custom"
			beam.Material = "Neon"
			beam.Transparency = 0.75
			beam.Anchored = true
			beam.Locked = true
			beam.CanCollide = false
			beam.CanQuery = false

			local distance = (tool.Handle.CFrame.p - position).magnitude
			
			beam.Size = Vector3.new(0.5, 0.5, distance)
			beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
			game:GetService("Debris"):AddItem(beam, 0.25)
			script.Laser:Play()
			for i = 1, 5 do
				beam.Size = beam.Size - Vector3.new(0.05,0.05,0)
				wait(0.05)
				if part then
					if part.Parent.Name == "StolenSoul" then
						print("Did Hit")
						local humanoid = part.Parent:FindFirstChild("Humanoid")

						if not humanoid then
							humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
						end
						if humanoid and debounce == false then
							humanoid:TakeDamage(5)
						end
						debounce = true
					else
						print("Didn't Hit")
						debounce = true
					end
				end
			end
			debounce = false
		end
	end)
end)

That’s probably because you’re dealing damage locally, rather than doing that on the server. (And a server-sided version of the script doesn’t work because the LocalPlayer is non-accessible to regular scripts)

What you could do, is fire a remote to the server specifying to deal damage to the NPC.

damageRemote.OnServerEvent:Connect(function(_, npcToDamage, damageAmount)
npcToDamage.Humanoid:TakeDamage(damageAmount)
end)

And in your weapon script:

damageRemote:FireServer(part.Parent, 5)

Note that this implementation isn’t very secure. The client (the user) can even specify other players that will receive damage, this example is merely meant to express how you can have the damage dealt replicate to the server so it functions properly.

Hope it helps, cheers.

1 Like

Thank you, you saved me from future headaches lol

This won’t really affect my game due to the fact that the place where this laser gun is in is very difficult to get to, so I don’t think it should be a problem, but regardless thanks for the warning.

1 Like