Raycasting not showing up

#1
tool.Activated:Connect(function()
	fire:Play()
	
	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)
end)

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

#2

I tried your script, and it seems to be working fine.
2018-08-06_16-58-12

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

script.Parent.Activated:Connect(function()
	Fire:Play()
	
	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)
end)

RobloxStudioBeta_2018-08-06_17-04-00

1 Like

#3

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

#4

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!

0 Likes

#5

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.

0 Likes

#6

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

0 Likes

#7

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.

0 Likes

#8

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

0 Likes

#9

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

#10

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?

0 Likes

#11

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

#12

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

0 Likes

#13

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

0 Likes

#14
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?

0 Likes