How do you detect if a person is moving, or jumping on a Mobile Device?

The question is very self explanitory, but I’m currently trying to make it so my Draggable Health Bar doesn’t drag when a player is using the mobile thumbstick, or jumping button on Mobile devices.

How would I go about doing this? I’m assuming it would be the UserInputService, but I’m not sure if I need to create a tag list of things that if pressed will ignore the health bar or something much more simple.

Code currently made

local UIS = game:GetService("UserInputService");local players = game:GetService("Players")
local TopBar = script.Parent;local Camera = game.Workspace:WaitForChild("Camera");local DragMousePosition,FramePosition;local Draggable = false
local UserId,thumbType,thumbSize = game.Players.LocalPlayer.UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420;local content, isReady = game:GetService("Players"):GetUserThumbnailAsync(UserId,thumbType,thumbSize)
local player = players.LocalPlayer;local char = player.Character or player.CharacterAdded:Wait();local hum = char:WaitForChild("Humanoid")
local Digit = script.Parent:WaitForChild("Holder").Digits;local MaxDigit = script.Parent.MaxHPDisplay.MaxDigits;local Healthbar = script.Parent:WaitForChild("Holder").Healthbar;local Avatar = script.Parent.Avatar_Icon
local colorHealthMax = Color3.fromRGB(0,255,0);local colorHealthMid = Color3.fromRGB(255,200,0);local colorHealthMin = Color3.fromRGB(255,0,0)
Avatar.Image = content

TopBar.InputBegan:Connect(function(input)
	if UIS and UIS:GetFocusedTextBox() == nil then
		if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
			Draggable = true
			DragMousePosition = Vector2.new(input.Position.X, input.Position.Y)
			FramePosition = Vector2.new(TopBar.Position.X.Scale,TopBar.Position.Y.Scale)
		end
	end
end)

TopBar.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		Draggable = false
	end
end)

UIS.InputChanged:Connect(function(input)
	if Draggable == true then
		local NewPosition = FramePosition + ((Vector2.new(input.Position.X,input.Position.Y) - DragMousePosition) / Camera.ViewportSize)
		TopBar.Position = UDim2.new(NewPosition.X,0, NewPosition.Y,0)
	end
end)

Hello, so first, you need to define these exclusion zones. You can do this by creating a list of Rect objects, each representing a “no-drag zone” on the screen. Each Rect object takes four parameters: minX, minY, maxX, maxY

local noDragZones = {
    -- Add the thumbstick and jump button exclusion zones here
    Rect.new(thumbstickMinX, thumbstickMinY, thumbstickMaxX, thumbstickMaxY),
    Rect.new(jumpButtonMinX, jumpButtonMinY, jumpButtonMaxX, jumpButtonMaxY)
}

Then, you can create a helper function to check whether a touch position is inside any of these exclusion zones:

function isInNoDragZone(position)
    for _, rect in ipairs(noDragZones) do
        if rect:contains(position) then
            return true
        end
    end
    return false
end

and you also need to modify your InputBegan event to use this helper function:

TopBar.InputBegan:Connect(function(input)
    if UIS and UIS:GetFocusedTextBox() == nil then
        if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
            if not isInNoDragZone(input.Position) then
                Draggable = true
                DragMousePosition = Vector2.new(input.Position.X, input.Position.Y)
                FramePosition = Vector2.new(TopBar.Position.X.Scale,TopBar.Position.Y.Scale)
            end
        end
    end
end)

If this works properly It should do when a player touches the thumbstick or jump button, it won’t trigger the draggable functionality of the health bar. Hope It helps

1 Like

This is a sound way to deal with this, however my health bar is for both PC, Console, and Mobile. So… putting in an exclusion zone would make it so I have to detect what platform a user is using… which I’m trying to avoid. Plus, with the Dynamic Thumbstick… they can use up to half of the screen as their thumbstick