Why does the same variable print different things in different functions?

function createRay(input)
    print(input) -- prints as a number like "0.014795400202274"
    wait(10)
    if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
        local unitray = workspace.Camera:ViewportPointToRay(input.Position.X,input.Position.Y)
        local ray = Ray.new(unitray.Origin, unitray.Direction * 500)
        local hit = workspace:FindPartOnRayWithIgnoreList(ray,IgnoreList)
        if hit then
            game.ReplicatedStorage.print:FireServer(hit.Name)    
        end
    end
end

function findHover(input,gameProcessedEvent)
    print(input) -- prints as InputObject
    if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
        print(input)
        debounceMouse = true
        createRay(input)
        debounceMouse = false
    end
end

Why does the first function print the number instead of the InputObject?
createRay() is also called from a BindToRenderStepped further down

RunService:BindToRenderStep("tempBinding", 2, createRay)

Are you calling createRay from anywhere else?

Yeah, I edited it in now, it’s calling from a BindToRenderStep event

It’s because the variable is defined in 2 DIFFERENT functions, which technically makes them two variables

It’s not though? The variable is only defined in InputChanged:Connect() function.

When you call createRay from findHover, input is an InputObject, as you desire.

It’s your binding of createRay to render step using RunService:BindToRenderStep that’s the issue. The only argument passed to functions bound to render step when they are called is the delta time between the last render step and the current render step, which is a number. That number becomes input when the function is called.

1 Like

ah, so to solve this, I’d basically have to define input as a defined variable outside of the function and just define it every time the InputChanged fires?

let’s have another example: if I were to have only one remote event, and have 2 different scripts firing the event, and have a server script to print the thing it fires. It wouldn’t print the same thing if the 2 scripts are different

Sure. I’d personally store just the position directly if you’re not going to use the other properties of the InputObject.

1 Like

Oh, that would make more sense lmao. I suppose that works better
Thank you!

Alright here’s the solution I came up with:

local mousePositionX
local mousePositionY
function createRay()
	print(mousePositionX.." "..mousePositionY)
	if mousePositionX then
		local unitray = workspace.Camera:ViewportPointToRay(mousePositionX,mousePositionY)
		local ray = Ray.new(unitray.Origin, unitray.Direction * 500)
		local hit = workspace:FindPartOnRayWithIgnoreList(ray,IgnoreList)
		if hit then
			game.ReplicatedStorage.print:FireServer(hit.Name)	
		end
	end
end

function findHover(input,gameProcessedEvent)
	print(input)
	if debounceMouse == false and input.UserInputType == Enum.UserInputType.MouseMovement and not gameProcessedEvent or debounceMouse == false and input.UserInputType == Enum.UserInputType.Touch and not gameProcessedEvent then
		print(input)
		debounceMouse = true
		mousePositionX = input.Position.X
		mousePositionY = input.Position.Y
		createRay()
		debounceMouse = false
	end
end

Basically I just define the X and Y positions in their own variables, and I just call them in the renderstep function, since the mouse position doesnt update, if say, only the camera moves!

1 Like