2D (GUI) Player Movement Template

,

Ever thought of working on a GUI-based game? 2d player? 2d world? 2d whatever? Allow me to help speed up your process!

In my spare time, I put together a simple script that gives your 2d player basic movement. A simple Left, Right, Jump script to give your project a little headstart.

Do note as of when I made this, I adapted it to a frame with a Y-Scale size of 0.125 accompanied by an AspectRatioConstraint set to an AspectRatio of 0.75. You will most likely need to adjust the 0.95 at lines 28, 29, & 50.

Demo place:
https://www.roblox.com/games/7088175631/Gui-Player-Movement-Demo

Model:
https://www.roblox.com/library/7088136063/Gui-Player-Movement

Raw Script (goes inside of your player gui object):

------------------------------------------------------------------------------ Variables
local uIS = game:GetService("UserInputService") 
local tS = game:GetService("TweenService")

local player = script.Parent

local leftOrRightTweenInfo = TweenInfo.new(0.1)
local jumpTweenInfo = TweenInfo.new(0.15)
local jumpFallInfo = TweenInfo.new(0.25)

local movingLeft = false
local movingRight = false
local jumping = false
local jumpMoveDirection = 0

local leftInputs = {Enum.KeyCode.A, Enum.KeyCode.Left, Enum.KeyCode.DPadLeft}
local rightInputs = {Enum.KeyCode.D, Enum.KeyCode.Right, Enum.KeyCode.DPadRight}
local jumpInputs = {Enum.KeyCode.W, Enum.KeyCode.Up, Enum.KeyCode.DPadUp}

------------------------------------------------------------------------------ Functions
local function tweenMovement(movementType, direction)
	tS:Create(player, movementType, {Position = player.Position + (direction)}):Play()
end

local function edgeClipping() -- Crude way of keeping player object on-screen
	if player.Position.X.Scale < 0 then
		player.Position = UDim2.new(0, 0, player.Position.Y.Scale)
	elseif player.Position.X.Scale > 0.95 then
		player.Position = UDim2.new(0.95, 0, player.Position.Y.Scale)
	end
end

------------------------------------------------------------------------------ Moving Left/Right
uIS.InputBegan:Connect(function(input)
	if not movingLeft and not movingRight then
		
		if table.find(leftInputs, input.KeyCode) then -- Left
			movingLeft = true
			while movingLeft and player.Position.X.Scale >= 0 do
				if not jumping then
					tweenMovement(leftOrRightTweenInfo, UDim2.new(-0.03))
					wait(0.1)
				else
					wait(0.05)
				end
			end
			
		elseif table.find(rightInputs, input.KeyCode) then -- Right
			movingRight = true
			while movingRight and player.Position.X.Scale <= 0.95 do
				if not jumping then
					tweenMovement(leftOrRightTweenInfo, UDim2.new(0.03))
					wait(0.1)
				else
					wait(0.05)
				end
			end
		end
	end
	edgeClipping()
end)

uIS.InputEnded:Connect(function(input)
	if movingLeft and table.find(leftInputs, input.KeyCode) then
		movingLeft = false
	elseif movingRight and table.find(rightInputs, input.KeyCode)then
		movingRight = false
	end
end)

------------------------------------------------------------------------------ Jumping
uIS.InputBegan:Connect(function(input)
	if not jumping then
		if table.find(jumpInputs, input.KeyCode) then
			jumping = true
			
			if movingLeft then
				jumpMoveDirection = -0.1
			elseif movingRight then
				jumpMoveDirection = 0.1
			else
				jumpMoveDirection = 0
			end
			
			tweenMovement(jumpTweenInfo, UDim2.new(jumpMoveDirection, 0, -0.1))
			wait(0.15)
			tweenMovement(jumpFallInfo, UDim2.new(jumpMoveDirection, 0, 0.1))
			
			wait(0.25)
			jumping = false
		end
	end
	edgeClipping()
end)
21 Likes

excellent tutorial this a little reminds the pacman :grin: :+1:

Might need it in the future, thanks! I gonna bookmark this