I'm experimenting with raycasting, and I made this script that is supposed to cast a ray towards the player whenever they click the part.
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
script.Parent.ClickDetector.MouseClick:Connect(function()
local player = plr
local ray = Ray.new(script.Parent.CFrame.p, char.HumanoidRootPart.CFrame.p)
local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)
local beam = Instance.new("Part", workspace)
beam.BrickColor = BrickColor.new("Royal purple")
beam.FormFactor = "Custom"
beam.Material = "Neon"
beam.Transparency = .25
beam.Anchored = true
beam.Locked = true
beam.CanCollide = false
local distance = (script.Parent.CFrame.p - position).magnitude
beam.Size = Vector3.new(.3,.3,distance)
beam.CFrame = CFrame.new(script.Parent.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
game:GetService("Debris"):AddItem(beam, .1)
end)
end)
end)
But for some reason, it casts it nowhere near me.

1 comment
Thanks