Why is this not casting the ray?

I’m working on a gun and this script isn’t creating a beam like I want it to.

-- Variables

local player = game.Players.LocalPlayer
local tool = script.Parent
local shootPart = tool.AKM.shootPart
local mouse = player:GetMouse()
local uiService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local equipped = false

-- Firing Functions

local function semiFire()
	
	local ray = Ray.new(shootPart.CFrame.p, (mouse.Hit.p - shootPart.CFrame.p).unit * 300)
	
	local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
	
	local beam = Instance.new("Part", workspace)
	beam.BrickColor = BrickColor.new("Really red")
	beam.FormFactor = "Custom"
	beam.Material = "Neon"
	beam.Transparency = 0.5
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false
	
	local distance = (shootPart.CFrame.p - position).magnitude
	
	beam.Size = Vector3.new(0.1, 0.1, distance)
	beam.CFrame = CFrame.new(shootPart.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
			local akmEvent = ReplicatedStorage.Guns.AKM:WaitForChild("akmEvent")
			akmEvent:FireServer(part, humanoid)
		end
	end
end

--Equip / Unequip

tool.Equipped:Connect(function(mouse)
	equipped = true
end)

tool.Unequipped:Connect(function(mouse)
	equipped = false
end)

-- Click

uiService.InputBegan:Connect(function(input)
	if equipped == true then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			semiFire()
			print("shoot@!!1!")
		end
	end
end)

uiService.InputEnded:Connect(function(input)
	if equipped == true then
		if input.UserInputType == Enum.UserInputType.MouseButton1 then
			print("stop shoot :(()()()")
		end
	end
end)

Are you getting any errors in the output? I recreated a gun with your script and got an error on line 5 (AKM is not a valid member of Tool). Changing line 5 to this fixed it and the beam fired with no issues.

local AKM = tool:WaitForChild("AKM")
local shootPart = AKM:WaitForChild("shootPart")

The error is caused because the script runs before the game has a cheance to create the gun parts, hence they dont exist when you define them and cause the error. WaitForChild solves this by waiting until the instance exists before proceeding.

There were no errors and this didn’t work, but thanks anyway :slight_smile:

Is this referring to the same issue as your last post? Or a different issue?

Edit: last response

I was having a lot of issues with that script and I had just started it so I decided to start over from scratch. It’s a completely different issue.

Could you describe what you mean by the beam isnt casting like you want?

The script is supposed to create a beam, but clicking doesn’t cast the beam (or ray). It also doesn’t fire to the server.

Does it print out that it was clicked? Also can you see any part of the beam?

I read over the code and it seems okay. I could definitely be missing something. Could you please put some print statements in to figure out where the code stops doing what you expect it to?

It does print out that it was clicked, yes. I can see the beam on the first click, but it doesn’t come from where I wanted it to (the gun).

If you want help, you need to provide all of the details that you have found while trying to get it working yourself.

Are you able to take a gif or picture (remove the debris) of what’s happening, still trying to figure out what’s wrong.

Is shootPart welded properly? If you want to attach/DM a repro file of the tool i can take a look at it. The code is good, just looks like its an issue with the gun model or possibly the server script. Here is the file of the test gun, the beam fires with no issue for me. TestGun.rbxm (4.7 KB)

2 Likes

You could try replacing the use of debris service with code like this:

delay(.1, function()
if beam and beam.Parent then
beam:Destroy()
end
end)

The place where it stopped printing was after if part then

It was an issue with the welding the entire time, I swear I remember welding it but I guess not. Thanks everyone for the help.

2 Likes

You also have a “WaitForChild” inside the fire function. You might want to replace that with a an already defined variable.

That’s just because you’re probably firing in the air or such. Unless you’re hitting a part, then theres another issue