How to check if holding down is out of bounds on a button?

I have this Motorcycle for mobile that uses ContexActionService it works but when I hold on to a control button and move my touch on different location while holding, the UserInputState is still not becoming an End

is there a way to determine if the user’s holding touch from a button went outside from its size?

This is my current script that doesn’t seem to work:

-- Some of actions does not have the isHovering because I was testing it first.
local function CASContext(actionName, state : Enum.UserInputState, inputObject)
	if state ~= Enum.UserInputState.Cancel then
		print(state)
		local Data = {
			Context = nil,
		}
		local isHovering = state == Enum.UserInputState.Change
		--print(isHovering)
		if actionName == _ACTION_NAMES[1] then
			-- Horn
			if state == Enum.UserInputState.Begin then
				Data.Context = "motorcycle-horn"
				MotorcycleRemote:FireServer(Data)
			end
		elseif actionName == _ACTION_NAMES[2] then
			-- Lights
			if state == Enum.UserInputState.Begin then
				Data.Context = "motorcycle-lights"
				MotorcycleRemote:FireServer(Data)
			end	
		elseif actionName == _ACTION_NAMES[3] then
			if state == Enum.UserInputState.Begin or isHovering then
				-- key down
				Data.Context = "motorcycle-forward"
				Data.State = "key-down"
				MotorcycleRemote:FireServer(Data)
			elseif state == Enum.UserInputState.End or not isHovering then
				-- key up
				Data.Context = "motorcycle-forward"
				Data.State = "key-up"
				MotorcycleRemote:FireServer(Data)
			end
		elseif actionName == _ACTION_NAMES[4] then
			if state == Enum.UserInputState.Begin then
				-- key down
				Data.Context = "motorcycle-reverse"
				Data.State = "key-down"
				MotorcycleRemote:FireServer(Data)
			elseif state == Enum.UserInputState.End then
				-- key up
				Data.Context = "motorcycle-reverse"
				Data.State = "key-up"
				MotorcycleRemote:FireServer(Data)
			end
		elseif actionName == _ACTION_NAMES[5] then
			if state == Enum.UserInputState.Begin and isHovering then
				-- key down
				Data.Context = "motorcycle-left"
				Data.State = "key-down"
				MotorcycleRemote:FireServer(Data)
			elseif state == Enum.UserInputState.End and not isHovering then
				-- key up
				Data.Context = "motorcycle-left"
				Data.State = "key-up"
				MotorcycleRemote:FireServer(Data)
			end
		elseif actionName == _ACTION_NAMES[6] then
			if state == Enum.UserInputState.Begin then
				-- key down
				Data.Context = "motorcycle-right"
				Data.State = "key-down"
				MotorcycleRemote:FireServer(Data)
			elseif state == Enum.UserInputState.End then
				-- key up
				Data.Context = "motorcycle-right"
				Data.State = "key-up"
				MotorcycleRemote:FireServer(Data)
			end
		end
	end
end

I think I’ve managed to fix the issue but I’m half asleep and intoxicated so I don’t know if it’ll work lol

local buttonSize = Vector2.new(100, 50) -- Example size of the button

local function isTouchInsideButton(touchPosition)
    local buttonPosition = Vector2.new(200, 200) -- Example position of the button
    local buttonMin = buttonPosition - buttonSize / 2
    local buttonMax = buttonPosition + buttonSize / 2
    return touchPosition.X >= buttonMin.X and touchPosition.X <= buttonMax.X
        and touchPosition.Y >= buttonMin.Y and touchPosition.Y <= buttonMax.Y
end

local function CASContext(actionName, state : Enum.UserInputState, inputObject)
    if state ~= Enum.UserInputState.Cancel then
        print(state)
        local Data = {
            Context = nil,
        }
        local touchPosition = inputObject.Position
        local isHovering = isTouchInsideButton(touchPosition)
        if actionName == _ACTION_NAMES[1] then
            -- Horn
            if state == Enum.UserInputState.Begin then
                Data.Context = "motorcycle-horn"
                MotorcycleRemote:FireServer(Data)
            end
        elseif actionName == _ACTION_NAMES[2] then
            -- Lights
            if state == Enum.UserInputState.Begin then
                Data.Context = "motorcycle-lights"
                MotorcycleRemote:FireServer(Data)
            end
        elseif actionName == _ACTION_NAMES[3] then
            if state == Enum.UserInputState.Begin or isHovering then
                -- key down
                Data.Context = "motorcycle-forward"
                Data.State = "key-down"
                MotorcycleRemote:FireServer(Data)
            elseif state == Enum.UserInputState.End or not isHovering then
                -- key up
                Data.Context = "motorcycle-forward"
                Data.State = "key-up"
                MotorcycleRemote:FireServer(Data)
            end
        -- Similarly handle other actions
        end
    end
end
1 Like

I don’t think this is an optimal way because there could be a multiple touch on the screen therefore, it is not that accurate. (Correct me if I’m wrong.)