Vector3 results in 0, 0, 0

  1. What do you want to achieve?

A fix to the issue.

  1. What is the issue?

The “directory.End.WorldPosition = origin - direction” of the source code below results in the end attachment location going to 0, 0, 0 when I click, but it should be, well, where the mouse was clicked. (it’s not the raycast because I tested the raycast with a print function and it read out where the mouse was clicked at)


-- "mousehit" is the mouse.hit.position
-- "End" is an attachment

        local origin = script.Parent.immolationdevice.Position
        local direction = mousehit

        local raycastParams = RaycastParams.new()

	raycastParams.FilterDescendantsInstances = {script.Parent}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true

	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
		
	firerun:GetMarkerReachedSignal("Fire"):Connect(function() 
		
		directory.End.WorldPosition = Vector3.new(origin - direction)
		
		directory.Beam.Enabled = true

	end)

(the green line coming from the orange cone on the weapon is the beam, the start of it is at the cone and the issue’d attachment, the end, is at 0, 0, 0; where the beam is heading to)

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve asked a friend for help and recommendations, and I’ve searched through a LOT results, at least 8 hours worth.

1 Like

correct me if im wrong i think it shouldve been:


directory.End.WorldPosition = raycastResult.Position

2 Likes

bruh why does the code looks so weird when i type it on mobile

1 Like

I tried that earlier, and I just tried it now. It gives me an error now and doesn’t show a beam: (on that same line) attempt to index nil with ‘Position’.

I read another devforum post that said a fix for this issue was to use “origin - direction” as the vector3 value.

1 Like

He’s right though. Thing is, when a Raycast doesn’t hit something, the RaycastResult is nil.

All you have to do is make it so it ends the code when the raycastResult is nil.

1 Like

But I clicked the ground before, which is the baseplate, wouldn’t it detect that?

1 Like

The direction is not from the Origin to the Mouse.Hit

You’d have to set direction to

CFrame.lookAt(origin, direction).LookVector * distance

The CFrame.LookAt() makes a new Cframe starting at the origon and pointing at the end. It’s LookVector is the direction of its front face. Finally, you multiply the LookVector by its maximum distance (in studs).

3 Likes

what about if you check like:

if raycastResult then

end
2 Likes

ive been through this before, i think the reason its nil is because theres no checking if the raycast had casted yet. so u need to check if the ray is already casted

1 Like

This helped out, thank you very much!

1 Like

This also helped lead up to the answer, thank you.

1 Like

I’m having a new issue now, the beam now goes to the floor anytime I click. If you need reference material, ask around 1 PM to 2 PM (EST UTC-6)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.