Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Best way to make a 2D Character move?

I have no idea how to make a 2d character move left and right when you hold D or A, ive tried using TweenService but it's confusing for me, what is the best way?

3 comments

0

What do you mean by 2D character?

0

An imagelabel that moves by pressing d or a

0

I'm currently working on a solution to your problem

1 answer

✓ Accepted answer

Since it looks like you're trying to use sprites, this may be what you're looking for.

local sprite = script.Parent
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local right = 0
local left = 0
local speed = 4 --Replace with whatever number you want

UserInputService.InputBegan:Connect(function(inputObj, processed)
	if (inputObj.KeyCode == Enum.KeyCode.Left or inputObj.KeyCode == Enum.KeyCode.A) then
		left = 1
	if (inputObj.KeyCode == Enum.KeyCode.Right or inputObj.KeyCode == Enum.KeyCode.D) then
		right = 1
	end
end)

UserInputService.InputEnded:Connect(function(inputObj, processed)
	if (inputObj.KeyCode == Enum.KeyCode.Left or inputObj.KeyCode == Enum.KeyCode.A) then
		left = 0
	if (inputObj.KeyCode == Enum.KeyCode.Right or inputObj.KeyCode == Enum.KeyCode.D) then
		right = 0
	end
end)

RunService.RenderStepped:Connect(function(step)
	sprite.Position = sprite.Position + UDim2.new(0, (right - left) * speed, 0, 0)
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.