What do you want to achieve?
I would like to create a visible raycast (using a part) between the player head and camera
to hopefully use to place a picked up box at the end of wherever clicked
What is the issue?
I am getting an error
" Players.BallOTinfoil.PlayerScripts.LocalScript:12: attempt to perform arithmetic (mul) on Instance and Vector3 "
when activating the raycast
What solutions have you tried so far?
My solution to try and fix the general problem was to create a visible raycast and place the box at the end of it. however, that hasn’t been working either
I have tried looking on the devforum to help but I’m unable to find a proper solution that I can actually understand
local beamradius = 1
-----------------------------------
local Holding = false
local Held = nil
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
------------------------------------
local function MakeRayVisible(ray)
local midpoint = player.Character.Head + mouse.Hit.p/2
local raypart = Instance.new("Part")
raypart.Parent = workspace
raypart.Anchored = true
raypart.CFrame = CFrame.new(midpoint, player.Character.Head)
raypart.Size = Vector3.new(beamradius, beamradius, mouse.Hit.p.Magnitude)
raypart.Material = Enum.Material.Neon
raypart.BrickColor = BrickColor.new("Light red")
return raypart
end
------------------------------------
local function performRaycast()
local ray = Ray.new(
player.Character.Head.Position,
(mouse.Hit.p - player.Character.Head.Position).unit * 20
)
-------------------------------------
if Holding == true then
MakeRayVisible()
Held.Position = mouse.Hit.p - player.Character.Head.Position
Held.PartToPlayerWeld:Destroy()
Holding = false
Held = nil
--------------------------------------
else
local Part, position = workspace:FindPartOnRay(ray, player.Character)
if Part then
MakeRayVisible()
Held = Part
print("Hit:".. Part.Name)
local weld = Instance.new("Weld")
weld.Name = "PartToPlayerWeld"
weld.Part0 = player.Character.Head
weld.Part1 = Part
weld.Parent = Part
Holding = true
end
end
end
mouse.Button1Up:Connect(performRaycast)
You could add a check before that if statement like:
if ray then
This will make sure that the ray actually hit something. The reason its erroring is because the ray doesn’t exist (this is the most reasonable and realistic answer)
So it no longer errors but the clicked part doesn’t get welded to the character’s head (just a temporary spot until everything actually works) robloxapp-20231021-2234040.wmv (583.5 KB)
and here’s a picture of the script with the statement added
It should already print the part name that was hit, what should be happening is it casts a ray from the character head to the mouse and the direction length is 50 studs so it should be finding something.
should I change the way i try to find the end of the raycast after already holding something?