Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Ray casting does not work in LocalScripts?

Asked by
WoTrox 345 Moderation Voter
4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
Does the same thing. No error codes, but i can't see the ray. Or shall I see it? WoTrox 345 — 4y
0
You have to create a part go to the link to learn how namespace25 594 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)

Answer this question