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.
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.
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?
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.