TicTacToe Game not working!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? An on-touch tictactoe game on Roblox! There will be a 45x45 studs block filled in more with 9 plates.

  2. What is the issue? It’s completely buggy and unstable!

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Nothing of this kind!

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local plates = script.Parent:FindFirstChild("Plates"):GetChildren()
local RS = game:GetService("ReplicatedStorage")
local db = false
local turn = 0

local function tictactoe_function(Plate)
	print(Plate)
	if turn == 9 then
		local OorX = script.Parent.Turn:GetChildren()
		for i, y in pairs(OorX) do
			y:Destroy()
		end
		for i, x in pairs(plates) do
			x:FindFirstChild("SelectionBox").Color3 = Color3.new(186, 186, 186)
		end
	end

	local existingO = script.Parent.Turn:FindFirstChild("O")
	local existingX = script.Parent.Turn:FindFirstChild("X")
	
	if not existingO or existingO.Position ~= Plate.Position + Vector3.new(0, 1, 0) or not existingX or existingX.Position ~= Plate.Position + Vector3.new(0, 1, 0) then
		if turn % 2 == 0 then
			print("Its O's Turn!")
			local O = RS:WaitForChild("O"):Clone()  -- Clone 'O' object inside the loop
			O.Parent = script.Parent:WaitForChild("Turn")
			O.Position = Plate.Position + Vector3.new(0, 1, 0)
			Plate:FindFirstChild("SelectionBox").Color3 = Color3.new(0, 0.666667, 1)
			
		else
			if not existingX or existingX.Position ~= Plate.Position + Vector3.new(0, 1, 0) or not existingO or existingO.Position ~= Plate.Position + Vector3.new(0, 1, 0) then
			print("Its X's Turn")
			local X = RS:WaitForChild("X"):Clone()  -- Clone 'X' object inside the loop
			X.Parent = script.Parent:WaitForChild("Turn")
			X.Position = Plate.Position + Vector3.new(0, 1, 0)
			Plate:FindFirstChild("SelectionBox").Color3 = Color3.new(1, 0, 0)
			end
		end
	end
end



while wait() do
	for i,v in pairs(plates) do
		if v:IsA("Part") then
			v.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and not db then
					db = true
					print("touched")

					if turn == 9 then
						turn = 0
					end

					turn += 1
					print(turn)
					tictactoe_function(v)
					wait(1)
					db = false
				end

			end)
		end
	end

end

Here are more screenshots for instance:

image (workspace)

image (Replicated Storage)

3 Likes