Raycasting not showing up

	local ray = Ray.new(shoot.CFrame.p, (mouse.Hit.p - shoot.CFrame.p).unit * 300)
	local part, position = workspace:FindPartOnRay(ray, character, false, true)

	local beam = Instance.new('Part', workspace)
	beam.BrickColor = BrickColor.new('Cyan')
	beam.FormFactor = 'Custom'
	beam.Material = 'Ice'
	beam.Transparency = 0.25
	beam.Anchored = true
	beam.Locked = true
	beam.CanCollide = false

	local distance = (shoot.CFrame.p - position).magnitude
	beam.Size = Vector3.new(0.3, 0.3, distance)
	beam.CFrame = CFrame.new(shoot.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

	game:GetService('Debris'):AddItem(beam, 0.1)

I’m just using the raycast on the Developer Hub, which directly specifies using LocalScript, but it doesn’t work so :man_shrugging:

1 Like


I tried your script, and it seems to be working fine.

Check your variables, that’s probably what’s causing the issue.

I used the same script you provided, but enhanced it a bit:

-- Services.
local Debris = game:GetService("Debris")

-- Main Variables.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse()

-- Other Variables.
local Shoot = script.Parent.Handle
local Fire = Shoot.Fire

	local ray = Ray.new(Shoot.CFrame.p, (Mouse.Hit.p - Shoot.CFrame.p).Unit * 300)
	local Part, Position = game.Workspace:FindPartOnRay(ray, Character, false, true)

	local Projectile = Instance.new("Part", game.Workspace)
	Projectile.BrickColor = BrickColor.new("Cyan")
	Projectile.FormFactor = Enum.FormFactor.Custom
	Projectile.Material = Enum.Material.Ice
	Projectile.Transparency = 0.25
	Projectile.Anchored = true
	Projectile.Locked = true
	Projectile.CanCollide = false

	local Distance = (Shoot.CFrame.p - Position).Magnitude
	Projectile.Size = Vector3.new(0.3, 0.3, Distance)
	Projectile.CFrame = CFrame.new(Shoot.CFrame.p, Position) * CFrame.new(0, 0, -(Distance / 2))

	Debris:AddItem(Projectile, 0.1)


1 Like


You testing online? It works in play solo, but not online. I put a print on every line and it prints everything when I click,

1 Like


This probably won’t solve your problem, but I noticed that in your code you parent the beam within the Instance.new() call.

Awhile ago it was discovered that this can lead to some serious efficiency problems and it is recommended that you parent objects after setting all of their properties, like this:

local beam = Instance.new('Part')
-- Set all of the properties like BrickColor, Anchored, etc.
beam.Parent = game.Workspace

Hopefully that will be helpful in the future!



Not sure if this will help or not but it may be a good idea to just put a wait() right before it’s moved to Debris and see if that changes anything.



That’s probably not the issue. And solving problems with waits always makes me feel like there should be a better way.



I, too, tested your script, only without Scriptily’s enhancements (just to see if those were fixing the problem) and I, too, found that your script works fine. I tested it in full client and server test mode instead of play solo as well.



Yeah, I’m not exactly well versed in ray casting. Just something I thought of.



Parts created in a LocalScript are seen only on the creator’s client, they don’t replicate to the server. If this is what you mean by not working online, the solutions are: Make the part on the server (will not feel responsive), or make the part on the creator’s client and also on the server, and then destroy the local part (or the new one) when the server one replicates to the creator’s client so they don’t see two copies.

1 Like


Is there a reason to do it this way over firing a remote event to all other clients to make the part on their clients?



In this special case, it probably does not make a huge difference because it’s presumably one part, non-collidable, for cosmetic display only. But, if you do something like this with a part that needs any physics simulation or collision detection to be consistent for all players or validated server-side, it would need to be done on the server.

1 Like


The dev hub should be editted then, because it specifically says to use in a LocalScript only or it will not work



Post a thread in #platform-feedback:documentation-requests detailing the issue with that page.


beam.CFrame = CFrame.new(shoot.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

I’ve done a lot of testing and basically removed every line one by one and replacing it with the original code, and this line is the line that ‘breaks’ it. If I change this line with the original line of code, it works to an extent (the ray just shoots down, instead of at the mouse)

beam.CFrame = CFrame.new(tool.Handle.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)

So for some reason, changing the item from handle to shoot breaks the gun?