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?
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
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)