Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Left and Right Key doesn't work for my snake game? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

I was making a Snake Game. For it to work I added a Local Script in the frame. The up and down works but the left and right didn't work.

The Local Script:

local chr = script.Parent.chr
local plrs = game:GetService("Players")
local plr = plrs.LocalPlayer
local plrmouse = plr:GetMouse()

plrmouse.KeyDown:Connect(function(key)
    if (key:byte() == 20) then --20 = Left Key
        if script.Parent.Visible == true then
            if chr.Position == not UDim2.new(0, 0, 0, 0) then
                chr.Position = chr.Position - UDim2.new(0, 30, 0, 0)
            end
        end
    elseif (key:byte() == 19) then --19 = Right Key
        if script.Parent.Visible == true then
            chr.Position = chr.Position + UDim2.new(0, 30, 0, 0)
        end
    elseif (key:byte() == 18) then --18 = Down Key
        if script.Parent.Visible == true then
            chr.Position = chr.Position + UDim2.new(0, 0, 0, 30)
        end
    elseif (key:byte() == 17) then --17 = Up Key
        if script.Parent.Visible == true then
            if chr.Position == not UDim2.new(0, 0, 0, 0) then
                chr.Position = chr.Position - UDim2.new(0, 0, 0, 30)
            end
        end
    end
end)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

just gonna say that it should be if not chr.Position == UDim2.new() instead of if chr.Position == not UDim2.new() and mouse.KeyDown is deprecated, so just use UserInputService

local UIS = game:GetService "UserInputService"
local player = game.Players.LocalPlayer

UIS.InputBegan:Connect(function(input,gameProcessedEvent)
    if gameProcessedEvent then return end
    if input.UserInputType == Enum.UserInputType.Keyboard then
        if input.KeyCode == Enum.KeyCode.Right then -- It could be RightArrow, im not sure and i don't remember
            -- Your code here
        end
    end
end)
0
Thanks but it didn't help. The problem is still there Filip100012 58 — 4y
0
Never mind, I just had to change the keybinds from Up, Down, Left, Right to U, J, H, K Filip100012 58 — 4y
Ad

Answer this question