Cant get nested function to change boolean with return

local function IsDragging() : boolean
	local Dragging
	local StartingMouseLocation = Vector2.new(Mouse.X, Mouse.Y)
	
	DragDetector = RenderStepped:Connect(function()
		
		if not UserInput:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then DragDetector:disconnect() 
			Dragging = false
			print("clicking")
			return Dragging
		end
		
		if UserInput:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) and (StartingMouseLocation - Vector2.new(Mouse.X, Mouse.Y)).Magnitude > 10 then
			DragDetector:disconnect()
			print("dragging")
			Dragging = true
			return Dragging
		end
		
	end)
	print(Dragging)
	return Dragging
end

dragging and clicking are printing at the right time so its working but i cant figure out how to return true or false; ive tried multiple ways

currently it doesnt update the dragging value and returns the default nil

Unless you’ve changed the setting (which I would not recommend), this connection will run as deferred. This means it is run in its own separate thread, which causes your code returning Dragging to run with your connected code. Therefore, Dragging is returned before it is assigned to, causing your issue.

You’ll need to make sure dragging is assigned to before returning it.

1 Like

Ok i think i understand
would i have to use a different method than renderstepped to make sure its assigned?

It might be better to use a while true iteration with a very short wait time, like 1/60th of a second. task.wait can wait as short as your frame rate, I’m pretty sure.

1 Like

Thank u I appreicacte it
jnhgvchb

1 Like

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