How would i make a gui frame move when i press a key?

hey, i want to make a gui-based 2d game on roblox, how would i make this?
i want to make a frame as the player, and i want him to move, the game would be a top-down game, this is what i have so far

(spoiler: it doesn’t work)

local plr = script.Parent
local spd = 3

game:GetService("UserInputService").InputBegan:Connect(function(input, processed)
	if not processed then
	
		if input == Enum.KeyCode.W then
			print("pressed w")
			plr.Position -= plr.Position * UDim2.new(0, 0, 0, spd)
		end
		
	end
end)

when i press w the print script doesn’t actually print
i’m also making this in a localscript just to be clear

  1. Make sure it’s in starter player scripts, second, it’s unclear if you’re trying to subtract the position multiplied by the using 2 from the position, but you first need a tonumber(instance) or the minus is a typo and it’s the entire reason it isnt working

but why the print isn’t printing in the output?

also, i just put in starterplayerscripts and it’s still not working

Create a local script at starter player scripts:

local UIS = game:GetService("UserInputService")

UIS.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.W then
	print("Key pressed")
	end
end)


UIS.InputEnded:connect(function(output)
	if output.KeyCode == Enum.KeyCode.W then
		print("Key released")
	end
end)

Hope this helps (:

1 Like

input.keycode, i forgot about that, maybe i’m dumb, anyway, how would i make the frame move, now this is the error:

Players.lucasvargasdebastian.PlayerScripts.LocalScript:9: attempt to perform arithmetic (mul) on UDim2

show me how your script looks like now…

local plr = game.Players.LocalPlayer.PlayerGui:WaitForChild('ScreenGui').Frame.Player
local spd = 3

game:GetService("UserInputService").InputBegan:Connect(function(input, processed)
	if not processed then
		
		if input.KeyCode == Enum.KeyCode.W then
			plr.Position += plr.Position * UDim2.new(0, 0, 0, -spd)
		end
		
	end
end)

I’m suspicious that you’re using chatgpt I don’t even capitalize UIS

no im not lol i just make it in a few seconds hehe

No one writes code like that for other people. And it’s a unspoken rule that you’re not supposed to use ai to help people,

Hi there, you seem to have forgotten one tiny aspect. The input variable is an InputObject type meaning that you cannot simply do if input == Enum.KeyCode.W then. You need to do if input.KeyCode == Enum.KeyCode.W then. Using the script from above, your final script should look like:

local Player = script.Parent
local speed = 3

game:GetService("UserInputService").InputBegan:Connect(function(input, processed)
	if not processed then
		if input.KeyCode == Enum.KeyCode.W then
			print("pressed w")
            --//Add your other code here
		end
	end
end)
1 Like

yes, i realized that, almost everything is done btw, i just needed to add input.KeyCode and remove += plr.Position * //rest of code

but now the frame only moves when i press the buttons multiple times, i want it to keep moving if i don’t release the button, but it isn’t working, it moves once and stops

btw, i want something like an arcade minigame, for it to move 3 pixels, stop for less than a second, move 3 pixels, and repeat it until i release the key

You can call a function or thread to do so and once you detect that the key is no longer pressed, you can cancel the thread or handle the function so that the code is no longer running.

btw, i want something like an arcade minigame, for it to move 3 pixels, stop for less than a second, move 3 pixels, and repeat it until i release the key

For this, I would use a repeat until loop with a task.wait(1) at the end. The until statement would be when IsKeyDown equals false.

1 Like

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