Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Ray casting does not work in LocalScripts?

I am making a gun, but Nothing happen, when I try to make a Ray from the gun to the mouse.Target

local head = game.Workspace[game.Players.LocalPlayer.Name].Head.CFrame.lookVector
local mmouse = CFrame.new(game.Workspace[game.Players.LocalPlayer.Name].Head.Position,mouse.Hit.p).lookVector
difference = (head-mmouse)
local ray = Ray.new(weapon.Shoot.CFrame.p, (mouse.Hit.p - weapon.Shoot.CFrame.p).unit * 300)

Is that because Ray casting doesn't work in LocalScripots? Or I missed something?

2 answers

You might have missed something? I'm not 100% sure because I don't use ray casting all that often but I can tell you it does work in local scripts.

Try this

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local ray = Ray.new(weapon.Shoot.Position, (mouse.Hit.Position - weapons.Shoot.Position))
local part, position = workspace:FindPartOnRay(ray, character)

You can go here for more information. Link

2 comments

0

Does the same thing. No error codes, but i can't see the ray. Or shall I see it?

0

You have to create a part go to the link to learn how

You are creating the raycast on the client. You didn't create a part on that raycast though. Raycasts are 2D objects when creating them, so it's impossible to visualize this in a 3D world, therefore, you have to create a part based on the 2D raycasts. (View suspectshot108's answer, line 6 for the actual code on how to do this)

This discussion is preserved as it was. New answers, comments, and votes are closed.