Hello, I'm experimenting with rays and i want the end of a part to point at my mouse but I'm having trouble, When I run this script it makes a part, but it goes in a random direction, Can anybody help? Thanks!
local tool = script.Parent
local deb = game:GetService("Debris")
script.Parent:WaitForChild('RemoteEvent').OnServerEvent:Connect(function(player,mouse,hit)
print("Making ray")
local ray = Ray.new(tool.CFrame.p, (hit - tool.CFrame.p).unit * 3000)
local part, position = workspace:FindPartOnRay(ray, tool, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.Red()
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = 0.25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
deb:AddItem(beam,1)
beam.Touched:Connect(function(hit)
print("Hit ".. hit.Name)
end)
local distance = (tool.CFrame.p - position).magnitude
beam.Size = Vector3.new(0.3, 0.3, distance)
beam.CFrame = CFrame.new(tool.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
print("Made")
end)
I know nothing about rays so i tried googling it but had no luck, Thanks for any help!
