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)