Need Help replicating the D-Pad to mobile

Hey! I’m trying to replicate the D-Pad from controllers because my games involves alot of the D-Pad.

So, I made 8 squares in a big square:

1=frame
0=empty space

1 1 1
1 01
1 1 1

Here’s the code:

while true do
	if DPad.Parent.Visible == true then	
		for i, v in buttons do
			v.MouseEnter:Connect(function()
				if v:GetAttribute("ButtonID") == 1 then --UP
					MoveX = 1
					MoveZ = 0
				end
				if v:GetAttribute("ButtonID") == 2 then --DOWN
					MoveX = -1
					MoveZ = 0
				end
				if v:GetAttribute("ButtonID") == 3 then --LEFT
					MoveZ = -1
					MoveX = 0
				end
				if v:GetAttribute("ButtonID") == 4 then --RIGHT
					MoveZ = 1
					MoveX = 0
				end
				if v:GetAttribute("ButtonID") == 5 then --UPLEFT
					MoveX = 1
					MoveZ = -1
				end
				if v:GetAttribute("ButtonID") == 6 then --UPRIGHT
					MoveX = 1
					MoveZ = 1
				end
				if v:GetAttribute("ButtonID") == 7 then --DOWNLEFT
					MoveX = -1
					MoveZ = -1
				end
				if v:GetAttribute("ButtonID") == 8 then --DOWNRIGHT
					MoveX = -1
					MoveZ = 1
				end
			end)
		end

		Hum:Move(Vector3.new(MoveX, 0, MoveZ), false) 
	end
	
	task.wait()
end

This basically makes the player move when touching 1 of the frames, but I haven’t found a way to make the player stop. I tried making the variables Move X and Z to 0 when the mouse leaves one of the frames, but it stoped when I hover my mouse to one frame and immediately going to the next. Any help is appreciated :).

1 Like

Hello, To create a D-Pad for mobile, you’ll want to handle both the MouseEnter and MouseLeave events for each button. The MouseEnter event will start the movement, and the MouseLeave event will stop it.

Here is the solution (I think :p)

Initialization :

local UserInputService = game:GetService("UserInputService")
local MoveX = 0
local MoveZ = 0
local buttons = {} -- Assuming you have initialized this with your D-Pad buttons
local DPad = buttons[1].Parent -- Assuming all buttons are children of the DPad
local Hum = -- Initialize this with your Humanoid or character's controller

Handling Button Events :

for _, v in pairs(buttons) do
    v.MouseEnter:Connect(function()
        -- Your existing code to set MoveX and MoveZ based on the button's attribute
    end)

    v.MouseLeave:Connect(function()
        MoveX = 0
        MoveZ = 0
    end)
end

Handling Touch End

UserInputService.TouchEnded:Connect(function()
    MoveX = 0
    MoveZ = 0
end)

Main Loop:

while true do
    if DPad.Visible then
        Hum:Move(Vector3.new(MoveX, 0, MoveZ), false)
    end
    task.wait()
end

Note: This setup should allow you to move the player when touching one of the frames and stop the movement when you stop touching or move your finger off the frame. If you move your finger from one button to another without lifting it, the TouchEnded event will ensure the movement stops when you finally lift your finger. I hope this helps! :slight_smile:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.