Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast Vector3 to float?

I'm having a problem where I repeatedly get the error "Unable to cast Vector3 to float" Whenever I run this script.

function TouchEnded(touch, gameProcessedEvent)
	RayPosition = touch.Position
	print("Touch ended at "..tostring(touch.Position))
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
	local Camera = workspace.CurrentCamera
	local rayDistance = 300
	local viewportPoint = Camera.ViewportSize / 2
	print(RayPosition)
	local unitRay = Camera:ViewportPointToRay(RayPosition, 0)
	local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance)

	part, hitPosition = workspace:FindPartOnRay(usableRay, char)
	if part then
		print("Hit part: " .. part:GetFullName())
	end
end)

The error appears on line 11. Any help will be appreciated

3 comments

0

the error is saying you can't enter a vector3 value to a float (I think)

0

That's as much as I know. I can't figure out how to fix it

0

That's why I didn't post an answer

1 answer

✓ Accepted answer

the function ViewportPointToRay doesn't accept vector3 values. It's arguments should be x,y,z and not Vector3(x,y,z). To fix this, just convert the vector3 to x,y,z like this:

function TouchEnded(touch, gameProcessedEvent)
    RayPosition = touch.Position
    print("Touch ended at "..tostring(touch.Position))
end

game:GetService("RunService").RenderStepped:Connect(function(dt)
    local Camera = workspace.CurrentCamera
    local rayDistance = 300
    local viewportPoint = Camera.ViewportSize / 2
    print(RayPosition)
    local unitRay = Camera:ViewportPointToRay(RayPosition.X, RayPosition.Y, 0)
    local usableRay = Ray.new(unitRay.Origin, unitRay.Direction * rayDistance)

    part, hitPosition = workspace:FindPartOnRay(usableRay, char)
    if part then
        print("Hit part: " .. part:GetFullName())
    end
end)

I'm just assuming you wanted the Z part to be 0

2 comments

0

Thank you. That's exactly what I was looking for.

0

No problem :)

This discussion is preserved as it was. New answers, comments, and votes are closed.