Why does this happen userInputService?

Here is my userInput code;

userInputService.InputBegan:Connect(function(input, gameProccessedEvent)
	if input.KeyCode == Enum.KeyCode.Space and not gameProccessedEvent and beingInspected then --StopAndStartRotatingItem
		local stopped
		stopped = input.Changed:Connect(function()
			if input.UserInputState == Enum.UserInputState.End then
				stopped:Disconnect()
				--//OFF
				LastMousePos = nil
				inspect_Rotate(targetsModelVariable, false)
				return LastMousePos
			end
		end)
		--//ON
		LastMousePos = Vector2.new(mouse.X,mouse.Y)
		inspect_Rotate(targetsModelVariable, true)
		return LastMousePos
	end
end)

and it calls this function;

local function inspect_Rotate(targetsModelVariable, check)
	runService.RenderStepped:Connect(function()
		if check then
			print("1")
		else
			print("2")
		end
	end)
end

when I hold space it prints 1 like it should but when I release it it’ll spam print 1 and 2 heres a video;

When you press spacebar, you call inspect_rotate once, which will create a RenderStepped connection and constantly print 1. When you release spacebar, you call that function one more time, without clearing the first connection.

Well you’re using runService.RenderStepped:Connect(function() < which is a loop function > when its called the first time itll always run. You call this function twice by first holding space > then when you let go of space > the function you have input.UserInputState == Enum.UserInputState.End > fires and activates that function a second time. So now you have 2 loops running both prints.

but there’s an if statement in there checking the value of a passed in variable that prints 1 or 2 based on if the variable is true or false.

This is what the first guy was saying then how would I disconnect the first loop?

why are you using a loop to begin with? Theres literally no reason for it.

That conditional statement is only checking the value of the parameter that you pass into the function. Parameters passed into a function are local to that function call only. Your second call of the function will never have any effect on your first call, because they are independent from one another.

well because the code will then rotate a model that needs to run every frame. But currently it is broken right now because of this issue.

Ahh very interesting this made a lot of sense so what if I make a global variable then set it before I run the function and return it. Then when the player stops pressing space set the variable and return it. I think that’s how I fixed it the first time. Would this work?

Its not broken, the script is doing exactly what you tell it to do lol.

Well I am redoing my code and making it more organized and turning it into object oriented programming. But the first time I ran into this problem I fixed it by making a global variable setting it then returning it I think that’s what fixed it.

I fixed the problem by making the check variable a global variable then set it when I called the function and returned it after calling the function.