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)