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
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
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)
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)
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)
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.