Need help with Match system (Complex code, for me)

GamePlay:
https://gyazo.com/3ff63f8541f0fe7037a856395b474553

Pls if you don’t understand tell me what you need and ill provide you if you don’t understand something its just my fault since this is not my main language.

So im working on a match system that currently doesn’t update the client thats why it doesn’t change anything in the ui rn.
The current problem is about me tring to switch from a crew to another
in the Output you can see a nil becosue its the first time i join a party

and its in the Code line:62
after there is a table which is the table printed at line:108 / 115
image

then its the time i try to change crew
Griffin which is the line 62
P1 from line 73 to see what i changed to
Change to see that i changed crew line 98
And from here it seems to work
BUT why is it PHOENIX it should be Griffin and after it get worse, like it reset.

image

Anchestry:

image

Module:

local Store = game:GetService("ReplicatedStorage")

local Match = {}
local Match1Players = {}
local Match2Players = {}

local Par = script.Parent
local last = nil

local Remotes = Store:WaitForChild("Remotes")

function isEmpty(t)
	return next(t) ~= nil
end
function Remove(Player,Crew)
	for i, v in pairs(Match1Players) do
		if v[1] == Player then
			if v[3] == Crew then
				return
			end
			for i, v in pairs(Match1Players) do
				if v[1] == Player then
					last = Crew
					table.remove(Match1Players,i)
					v = nil
				end
			end
		end
	end
	for i, v in pairs(Match2Players) do
		if v[1] == Player then
			if v[3] == Crew then
				return
			end
			for i, v in pairs(Match2Players) do
				if v[1] == Player then
					last = Crew
					table.remove(Match2Players,i)
					v = nil
				end
			end
		end
	end
end
function Match:StartLoop(Match)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == false then
			Par[Match.."On"].Loop.Value = true
			if Match == "Match1" then
				Match1Players = {}
			elseif Match == "Match2" then
				Match2Players = {}
			end
		end
	end
end

function Match:EnterLoop(Match,Player,Crew,Class)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == true then
			Remove(Player,Crew)
			print(last)
			if last then
				if Match == "Match1" then
					if Crew == "Phoenix" then
						print("G1")
						local plr = {}
						plr[1] = Player
						plr[2] = Class
						plr[3] = "Griffin"
						table.insert(Match1Players,plr)
					elseif Crew == "Griffin" then
						print("P1")
						local plr = {}
						plr[1] = Player
						plr[2] = Class
						plr[3] = "Phoenix"
						table.insert(Match1Players,plr)
					end
				elseif Match == "Match2" then
					if Crew == "Phoenix" then
						print("G2")
						local plr = {}
						plr[1] = Player
						plr[2] = Class
						plr[3] = "Griffin"
						table.insert(Match2Players,plr)
					elseif Crew == "Griffin" then
						print("P2")
						local plr = {}
						plr[1] = Player
						plr[2] = Class
						plr[3] = "Phoenix"
						table.insert(Match2Players,plr)
					end
				end
				last = nil
				print("Change")
				print(Match1Players)
				return
			end
			if Match == "Match1" then
				local plr = {}
				plr[1] = Player
				plr[2] = Class
				plr[3] = Crew
				table.insert(Match1Players,plr)
				print(Match1Players)
			elseif Match == "Match2" then
				local plr = {}
				plr[1] = Player
				plr[2] = Class
				plr[3] = Crew
				table.insert(Match2Players,plr)
				print(Match2Players)
			end
		end
	end
end
function Match:ExitLoop(Match,Player)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == true then
			print("Exit")
			if Match == "Match1" then
				for i, v in pairs(Match1Players) do
					if v[1] == Player then
						table.remove(Match1Players,i)
						v = nil
						print(Match1Players)
					end
					if isEmpty(Match1Players) then
						Par.Match1On.Value = false
						Par.Match1On.Loop.Value = false
					end
					if isEmpty(Match2Players) then
						Par.Match2On.Value = false
						Par.Match2On.Loop.Value = false
					end
				end
			elseif Match == "Match2" then
				for i, v in pairs(Match2Players) do
					if v[1] == Player then
						table.remove(Match2Players,v)
						v = nil
						print(Match2Players)
					end
				end
				if isEmpty(Match1Players) then
					Par.Match1On = false
					Par.Match1On.Loop = false
				end
				if isEmpty(Match2Players) then
					Par.Match2On = false
					Par.Match2On.Loop = false
				end
			end
		end
	end
end

return Match

I just need help with fixing and improving the code, i’ve been trying to fix this for about 5 hours i don’t want to quit the project since its the first time im actually making good things and im motivated. i don’t want to lose motivation pls.

2 Likes

I feel like im dumb rightnow, when i was working on the code i tried to make things more complex then i thought they would have beenfor anyone interested here is the fixed code, im realy stupid for who ever read what i writed above :

local Store = game:GetService("ReplicatedStorage")

local Match = {}
local Match1Players = {}
local Match2Players = {}

local Par = script.Parent

local Remotes = Store:WaitForChild("Remotes")

function isEmpty(t)
	return next(t) ~= nil
end
function Remove(Player,Crew)
	for i, v in pairs(Match1Players) do
		if v[1] == Player then
			for i, v in pairs(Match1Players) do
				if v[1] == Player then
					table.remove(Match1Players,i)
					v = nil
				end
			end
		end
	end
	for i, v in pairs(Match2Players) do
		if v[1] == Player then
			for i, v in pairs(Match2Players) do
				if v[1] == Player then
					table.remove(Match2Players,i)
					v = nil
				end
			end
		end
	end
end
function Match:StartLoop(Match)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == false then
			Par[Match.."On"].Loop.Value = true
			if Match == "Match1" then
				Match1Players = {}
			elseif Match == "Match2" then
				Match2Players = {}
			end
		end
	end
end

function Match:EnterLoop(Match,Player,Crew,Class)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == true then
			Remove(Player,Crew)
			if Match == "Match1" then
				local plr = {}
				plr[1] = Player
				plr[2] = Class
				plr[3] = Crew
				table.insert(Match1Players,plr)
				print(Match1Players)
			elseif Match == "Match2" then
				local plr = {}
				plr[1] = Player
				plr[2] = Class
				plr[3] = Crew
				table.insert(Match2Players,plr)
				print(Match2Players)
			end
		end
	end
end
function Match:ExitLoop(Match,Player)
	if Par[Match.."On"].Value == false then
		if Par[Match.."On"].Loop.Value == true then
			print("Exit")
			if Match == "Match1" then
				for i, v in pairs(Match1Players) do
					if v[1] == Player then
						table.remove(Match1Players,i)
						v = nil
						print(Match1Players)
					end
					if isEmpty(Match1Players) then
						Par.Match1On.Value = false
						Par.Match1On.Loop.Value = false
					end
					if isEmpty(Match2Players) then
						Par.Match2On.Value = false
						Par.Match2On.Loop.Value = false
					end
				end
			elseif Match == "Match2" then
				for i, v in pairs(Match2Players) do
					if v[1] == Player then
						table.remove(Match2Players,v)
						v = nil
						print(Match2Players)
					end
				end
				if isEmpty(Match1Players) then
					Par.Match1On = false
					Par.Match1On.Loop = false
				end
				if isEmpty(Match2Players) then
					Par.Match2On = false
					Par.Match2On.Loop = false
				end
			end
		end
	end
end

return Match

2 Likes