NPC Gun shoot problem, (function won't run)

Hi! I am trying to make an AI that fires lasers when it is near a player.


Here is my script

wait(7)
while true do
	if script.Parent.Humanoid.Health <= 0 then
		wait(3)
		script.Parent:Destroy()
		break
	end
	local SelfPosition = script.Parent.HumanoidRootPart.Position
	wait(0.00000001)
	math.randomseed(tick())
	local Move = math.random(1, 4)
	
	for i, player in ipairs(game.Players:GetChildren()) do
			local hahadistance = (player.Character:WaitForChild("HumanoidRootPart").Position - script.Parent.HumanoidRootPart.Position).Magnitude
			local radius = 50
			local closeRadius = 5
			follow = false
			if hahadistance <= radius then
				script.Parent.Humanoid.WalkToPoint = player.Character.HumanoidRootPart.Position
				local place = player.Character.HumanoidRootPart
				if hahadistance <= closeRadius then
					local tool = script.Parent["Laser Rifle"]
					local canFire = true
					local maxDistance = 500
					local damage = 51
					local shootTimer = 1
					local color = "Really blue"
					local selfselfself = script.Parent

					local GlobalFunction = require(game.ServerScriptService.GlobalFunctions)

					--fire laser function that runs off of the FireLaser remote event
					function fireLaser()
						--only fire laser if canFire is true
						if canFire == true then
							GlobalFunction.shootLaserAIAIAI(tool, maxDistance, damage, color, place, selfselfself)
							canFire = false
							wait(shootTimer)
							canFire = true
						end
					end
				end
			end	
	end
end

and this is the script inside of a global function that shoots the laser

        function GlobalFunctions.shootLaserAIAIAI(tool, maxDistance, damage, color, place, selfselfself)
		--create the ray itself from the tip to the position our mouse is clicking
		local ray = Ray.new(tool.Tip.CFrame.p, (place.p - tool.Tip.CFrame.p).unit * maxDistance)
		local part, position = workspace:FindPartOnRay(ray, selfselfself, false, true)
		--create the look of the beam
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new(color)
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
		--cap the distance to the maxDistance value
		local distance = (tool.Tip.CFrame.p - position).magnitude
		if (distance > maxDistance) then
			distance = maxDistance
		end
		--change the size and direction of the beam
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(tool.Tip.CFrame.p, position) * CFrame.new(0, 0, -distance/2)
		--destroy the beam after some timer
		game:GetService("Debris"):AddItem(beam, 0.1)
		--check if part is hit and if the part belongs to a humanoid
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
			--check another parent level if it has a humanoid
			if not humanoid then
				if not part.Parent.Parent:FindFirstChild("Gun Shield") then
					humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
				end
				
			end
			--do damage if humanoid
			if game.Workspace.TEAM.Value == false then
				if humanoid then
					local character = part.Parent
					if not character:FindFirstChild('Gun Shield') then
						humanoid:TakeDamage(damage)
					end
				end
			end	
		end
		
	end

I am not sure why some of that script isn’t showing up correctly, but it is pretty straight forward. Sorry about that.
I am getting no errors and it isn’t working, why won’t it fire?

Hi, you can post properly formatted code in two ways: either put 4 spaces before every line, or surround each block of code with
```
code
```

which gets formatted as

code

I’d also encourage you to try debugging it yourself using Studio’s debugging tools. Set a breakpoint near the start of the script, then step through each line, inspect the variables, and see why the laser doesn’t fire.

If this doesn’t work or it’s too overwhelming, let me know in a comment and I’ll take a better look when I have time :slight_smile:

Ok, thanks, I will look in to that.

Ok, I looked at that. I put some break points on the script, and for some weird reason it just stops on
function fireLaser() …

local GlobalFunction = require(game.ServerScriptService.GlobalFunctions)

						--fire laser function that runs off of the FireLaser remote event
						 function fireLaser()
							--only fire laser if canFire is true
							if canFire == true then
								GlobalFunction.shootLaserAIAIAI(tool, maxDistance, damage, color, place, selfselfself)
								canFire = false
								wait(shootTimer)
								canFire = true
							end
						end

And that isn’t making sense to me. Any ideas?