How can i make a better uis event

I want to make it so every time the player holds the screen for a bit of time, the functions will run and for every time the player stops holding the screen i want a different function to run. But whenever i try this, it never works out correctly. When i hold the screen sometimes it doesn’t work but other times it does. And the TouchEnded event isn’t really efficient. How can i use a better method?

local touching = script:WaitForChild("touching")

local debounce = 0
local debounceEnded = 0

UIS.TouchLongPress:Connect(function(tapPos,state, gameProcessedEvent)
	if gameProcessedEvent == true then return end
	if os.clock() - debounce < 2 then return end
	debounce = os.clock()
	print("Touched")
	touching.Value = true
end)

UIS.TouchEnded:Connect(function(input, gameProcessedEvent)
	if gameProcessedEvent == true then return end
	if os.clock() - debounce < 2 then return end
	debounce = os.clock()
	print("Touched")
	touching.Value = true
end)



touching.Changed:Connect(function()
	if touching.Value == false then
		keyUp()
		print("false")
	else
		keyDown()
	end
end)
1 Like

Is your game mobile-only?
I saw the TouchLongPress, so i assume it’s mobile only.

Have you checked what the state is meant to be used for? From what I can tell, perhaps it’s meant to show the status of the hold? Just my thoughts, I never really used it so.

Perhaps what you should do is check if the state function actually does that and if it does, then do while state == true do. That would save a lot of coding lines.

1 Like

the thing is state isn’t a bool. But if it was i would have used it

2 Likes

it’s both pc and mobile

1 Like