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

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

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

✓ Accepted answer

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)

2 comments

0

Thanks but it didn't help. The problem is still there

0

Never mind, I just had to change the keybinds from Up, Down, Left, Right to U, J, H, K

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