So basically, I'm trying to make a snake game and I want the snake to move in the direction of the button you pressed until you hit another key. I tried using repeat but it crashed my game so I'm not really sure where to go for there any help? My code is below ty :D

local board = game.Workspace.SnakeGame
local start = game.Workspace.Start
local clickdec = start.ClickDetector
local UIA = game:GetService("UserInputService")
local head = Instance.new("Part")

clickdec.MouseClick:Connect(function(touch)
	head.Parent = workspace
	head.Position = Vector3.new(-47.935, 23.075, -57.975)
	head.Size = Vector3.new(2.39, 2.31, 0.05)
	head.BrickColor = BrickColor.new("Gold")
	head.Anchored = true

end)

UIA.InputBegan:Connect(function(touch)
	if touch.KeyCode == Enum.KeyCode.Space
		then head.Parent = game.ServerStorage
		end
	
	repeat
	if touch.KeyCode == Enum.KeyCode.W
		then head.Position = head.Position + Vector3.new(0, 1,0)
		end
		if touch.KeyCode == Enum.KeyCode.S
		then head.Position = head.Position + Vector3.new(0, -1,0)
		end	
		if touch.KeyCode == Enum.KeyCode.D
		then head.Position = head.Position + Vector3.new(1, 0,0)
		end	
		if touch.KeyCode == Enum.KeyCode.A
		then head.Position = head.Position + Vector3.new(-1, 0,0)
		end
	until UIA.TouchEnded	
		
			
end)

2 comments

0

Create 4 booleans on, "right, left, down and up", make sure InputBegan turns the Boolean on, also make sure once it is on a loop makes the head move in the direction based on the positions, use InputEnded for when the player releases the key and also make sure that once they released the key, the direction of the snake should be travelling in its current path.

0

Not sure what you mean