Movement not working on snake game with second game

So I’m having a weird issue with my snake game I’m currently working on. I think it’s because of the fact that the coroutine is holding on to the deleted value. That’s what I seem to be seeing in the resume section of the coroutine page, but I may be wrong. Anyway, I’m trying to currently move the head of the snake, but resetting the game deletes the old head and then spawns a new one in when the player presses “Play”. Anyway, if it is the fact that the coroutine is holding on to the old head as it’s first parameter, what would you suggest I do to fix that issue?

Below, I’ve included all lines of code, the relevant ones should be starting at line 59, but I’ve included the rest if you have any questions about how it works:

repeat wait() until game.Players.LocalPlayer ~= nil
local player = game.Players.LocalPlayer
local clientModule = require(script.ClientModule)
local UIS = game:GetService("UserInputService")

local gameFrame = player.PlayerGui:WaitForChild("MainGui").GameFrame

local direction
local speed = 1
local startingPosition = UDim2.new(0,0,0,0)

local function endGame()
	gameFrame.PlayButton.BackgroundColor3 = Color3.new(0,255,0)
	gameFrame.PlayButton.Text = "Play"
	if gameFrame.SnakeFrame:FindFirstChild("Apple") ~= nil then
		gameFrame.SnakeFrame.Apple:Destroy()
	end
	if gameFrame.SnakeFrame.Snake:FindFirstChild("SnakeHead") ~= nil then
		gameFrame.SnakeFrame.Snake.SnakeHead:Destroy()
	end
	if gameFrame.SnakeFrame.Snake:FindFirstChild("SnakeBody") ~= nil then
		for _, bodyPart in pairs (gameFrame.SnakeFrame.Snake:GetChildren()) do
			if bodyPart.Name == "SnakeBody" then
				bodyPart:Destroy()
			end
		end
	end
end

local function spawnApple(apple)
	local randomX = math.random(0,9)
	local randomY = math.random(0,9)
	apple.Position = UDim2.new(0,(randomX*50),0,(randomY*50))
	apple.Parent = gameFrame.SnakeFrame
	apple.Visible = true
end

local function move(head)
	if head ~= nil then
		local function template(differentPositionInformation)
			local destination = head.Position + differentPositionInformation
			head.Position = head.Position + differentPositionInformation
		end
		if direction == "Up" then
			template(UDim2.new(0,0,0,-50))

		elseif direction == "Down" then
			template(UDim2.new(0,0,0,50))

		elseif direction == "Left" then
			template(UDim2.new(0,-50,0,0))

		elseif direction == "Right" then
			template(UDim2.new(0,50,0,0))
		end
	end
end

local snakeMovement = coroutine.create(function(head)
	while true do
		print("Looped")
		if gameFrame.PlayButton.BackgroundColor3 ~= Color3.new(1, 0, 0) then
			print("Broken")
			break
		end
		if direction ~= nil then
			print("There was a direction and worm will start the move loop")
			move(head)
		end
		wait(.5)
	end
end)

--When play button is clicked, start game
local playButton = gameFrame.PlayButton
playButton.MouseButton1Click:Connect(function()
	if playButton.BackgroundColor3 == Color3.new(0, 255, 0) then
		playButton.BackgroundColor3 = Color3.new(1, 0, 0)
		playButton.Text = "Game in progress"
		local head = gameFrame.References.SnakeHead:Clone()
		local body = gameFrame.References.SnakeBody:Clone()
		local apple = gameFrame.References.Apple:Clone()
		print(head)
		head.Position = startingPosition
		head.Parent = gameFrame.SnakeFrame.Snake
		head.Visible =  true
		spawnApple(apple)
		coroutine.resume(snakeMovement,head)
	else
		endGame()
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.Left then
			direction = "Left"
		elseif input.KeyCode == Enum.KeyCode.Right then
			direction = "Right"
		elseif input.KeyCode == Enum.KeyCode.Up then
			direction = "Up"
		elseif input.KeyCode == Enum.KeyCode.Down then
			direction = "Down"
		end
	end
end)

If you think it is a deleted value, can you show me the output from the script and the line it leads to

I don’t entirely understand what you mean by “the output from the script”. Can you clarify?
The function endGame deletes snake parts and apples in the game:

local function endGame()
	gameFrame.PlayButton.BackgroundColor3 = Color3.new(0,255,0)
	gameFrame.PlayButton.Text = "Play"
	if gameFrame.SnakeFrame:FindFirstChild("Apple") ~= nil then
		gameFrame.SnakeFrame.Apple:Destroy()
	end
	if gameFrame.SnakeFrame.Snake:FindFirstChild("SnakeHead") ~= nil then
		gameFrame.SnakeFrame.Snake.SnakeHead:Destroy()
	end
	if gameFrame.SnakeFrame.Snake:FindFirstChild("SnakeBody") ~= nil then
		for _, bodyPart in pairs (gameFrame.SnakeFrame.Snake:GetChildren()) do
			if bodyPart.Name == "SnakeBody" then
				bodyPart:Destroy()
			end
		end
	end
end

Is there a error in the output that leads to the script?

Oh no there’s no errors, only the necessary prints.

  SnakeHead  -  Client - MainClient:83
  17:49:26.137   ▶ Looped (x5)  -  Client - MainClient:61
  17:49:28.464  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:29.009  Looped  -  Client - MainClient:61
  17:49:29.014  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:29.540  Looped  -  Client - MainClient:61
  17:49:29.544  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:30.086  Looped  -  Client - MainClient:61
  17:49:30.089  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:30.601  Looped  -  Client - MainClient:61
  17:49:30.602  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:31.131  Looped  -  Client - MainClient:61
  17:49:31.133  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:31.644  Looped  -  Client - MainClient:61
  17:49:31.646  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:32.177  Looped  -  Client - MainClient:61
  17:49:32.179  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:32.701  Looped  -  Client - MainClient:61
  17:49:32.704  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:33.248  Looped  -  Client - MainClient:61
  17:49:33.253  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:33.792  Looped  -  Client - MainClient:61
  17:49:33.793  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:34.325  Looped  -  Client - MainClient:61
  17:49:34.331  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:34.866  Looped  -  Client - MainClient:61
  17:49:34.867  There was a direction and worm will start the move loop  -  Client - MainClient:67
  17:49:35.379  Looped  -  Client - MainClient:61
  17:49:35.381  Broken  -  Client - MainClient:63
  17:49:36.442  SnakeHead  -  Client - MainClient:83
  17:49:48.083  Disconnect from ::ffff:127.0.0.1|35498  -  Studio

@ItsMeFelixAccept Alright I know that’s the issue now. When it prints the head value in the coroutine, the first time, it shows the path the the head, and the second time, it highlights no objects, meaning that it’s still looking at the old one. Any suggestions for how to update the value or use something instead of a coroutine?

1 Like