Something Is Breaking My script

Whenever I fire out of the 300 stud Raycast radius the whole fire script breaks and I can’t figure out why. I am clicking after the beam stops showing but it is not working. I have tried to look up returning if the part is not found but it does nothing.

Edit: I just realised that I was using a different script from something else still doesn’t work.

--Variables
local Wand = script.Parent
local Tip = script.Parent.Handle.End

local mouse = plr:GetMouse()
local Firing = false
local player = game:GetService("Players").LocalPlayer
--color of lightning
local cole = plr.Temp.cole.Value
--Equiped Or Not
Wand.Equipped:Connect(function()
	equipped = true
end)

Wand.Unequipped:Connect(function()
	equipped = false
end)
--Main
mouse.Button1Down:Connect(function()
	if equipped and not Firing then
		Firing = true
		print("Clicked")
		
		local ray = Ray.new(Wand.Handle.End.CFrame.p, (mouse.Hit.p - Wand.Handle.End.CFrame.p).unit * 300)
		local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
		
 		local cole = player.Temp.cole.Value
		local beam = Instance.new("Part", workspace)
		beam.BrickColor = BrickColor.new(cole)
		beam.FormFactor = "Custom"
		beam.Material = "Neon"
		beam.Transparency = 0.25
		beam.Anchored = true
		beam.Locked = true
		beam.CanCollide = false
 	
		local distance = (Wand.Handle.End.CFrame.p - position).magnitude
		beam.Size = Vector3.new(0.3, 0.3, distance)
		beam.CFrame = CFrame.new(Wand.Handle.End.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

		game:GetService("Debris"):AddItem(beam, 0.1)
		if part then
			local humanoid = part.Parent:FindFirstChild("Humanoid")
 		
			if not humanoid then
				humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
			end

			if humanoid then
				(Something Else That Works)
			end
		wait(1)
		Firing = false
		end
	end
end)

[Solved]
Thanks for the quick responses I put the Firing = false in the wrong place. :smile:

1 Like

try adding a variable for getting the mouse instead of doing a parameter

local mouse = player:GetMouse()

This isn’t necessary because the exact object that GetMouse returns is passed internally in the Equipped event. That won’t change the outcome of this. As for what is happening: I don’t know.

2 Likes

I believe your issue is your placement of Firing = false. It’s within the if part then statement, which means that if there’s no part Firing would never be reset. You can solve this by moving specifically the Firing = false line after the if statement if you don’t want any reload delay to happen, or move it with the wait call if you’d still want the reload delay.

It also looks like these two lines are outside of the if statement due to their indentation but if you look at every if statement and match up the next end keyword you can see it is in the if statement. (The first thing I saw was an end immediately after those lines)

1 Like

I’m not sure the exact issue but have you tried:
local hit = mouse.Hit
if hit then
–code
end