Trying to create a pick up and place script using raycasts but running into errors

  1. 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

  1. 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

  1. 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)
4 Likes

First of all, I wouldn’t recommend using the Ray library. The most recent version would be workspace:Raycast()

I think you mean player.Character.Head.CFrame

That seemed to have helped somewhat, I’m now getting the error “Unable to cast RaycastResult to Ray” though

1 Like

On what line? Its kinda late for me and it’s kinda difficult to find the line

(Send the line)

Yeah it doesn’t give me the line causing the error…

Could you perhaps show a picture of the output?

image

2 Likes

It says the line number right there: Client - LocalScript:44

Could you send that specific line?

1 Like

sorry, i’m blind

2 Likes

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)


I added the check statement but it still gives me the same error

2 Likes

I’m sorry I meant add it before this if statement, was gonna specify

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

2 Likes

Well that means that the Part variable does not have an actual value assigned to it (which means workspace:FindPartOnRay() didn’t find anything)

Try printing out Part.Name and stuff like that. You should be able to find out the issue pretty easily yourself from here

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?

Probably, but yeah I don’t know if you’ve made this much then surely it’ll be almost as easy to debug it

A lot of it was made with the use of tutorials, I’m not that good at scripting.

the raycasting and welding of the clicked box was working though before I started trying to work on putting down the boxes

Mhm, yeah that’s okay I used to use a lot of tutorials too.

I’m not gonna be on here for much longer so I don’t think ima be able to continue at least for tonight


i think changing circle 1 to what it is now caused the function in circle 2 to break.


that’s okay, I’ll keep trying by myself to hopefully fix it

1 Like

Probably, maybe try switching it back I guess and see how that turns out