Hello, I’ve been trying to work on my match code, and it’s kind of making me annoyed, but I finally came to a working version and I’d like to get some help with how I can maybe make it a bit cleaner.
Code Explination: I’ve decided to use collection service for tagging the players/ai as player1 or player2 as well as what the opponent should be and the reason for using collection service is because it replicates and so on the client all I have to do is CS:GetTagged(localplayer.Name…“Opponent”)[1] (which would be player2). And lastly I’ve used an OOP approach for creating a new match and use loleris replica module to replicate it to the client for whenever something changes such as the match state.
local function SetupCharacter(character)
if not character then return end
warn("[SMM] Setting up character")
CS:AddTag(character.HumanoidRootPart,"HRPForCam")
if CS:HasTag(character,"Player1") then CS:AddTag(character,CS:GetTagged("Player2")[1].Name.."Opponent") end
if CS:HasTag(character,"Player2") then CS:AddTag(character,CS:GetTagged("Player1")[1].Name.."Opponent") end
--might come up with another method instead of using int values
Utils:MakeTag("Stun",character,"IntValue")
Utils:MakeTag("Guage",character,"IntValue")
Utils:MakeTag("Guard",character,"IntValue",1000)
--TO-DO
--1) add hurtbox & grabbox
warn("[SMM] Finished setting up character")
end
function SMM.new()
warn("[Server Match Manager] Creating a new match")
local instance = {}
local player1 = Players:GetPlayers()[1] or AI.new()
local player2 = Players:GetPlayers()[2] or AI.new()
CS:AddTag(player1.Character,"Player1")
CS:AddTag(player2.Character,"Player2")
SetupCharacter(CS:GetTagged("Player1")[1])
SetupCharacter(CS:GetTagged("Player2")[1])
--All of this will be getting replicated to the client using the replica module
instance.stage = StageManager:SpawnStage(nil,true)
instance.totalRounds = 3
instance.currentRound = 1
instance.time = 99
--instance.playersInMatch = { now that I'm using collection service I don't think I really need to be using this
-- player1 = player1,
-- player2 = player2,
--}
instance.state = FSM.create({
initial = "idle",
events = {
{name = "begin", from = "*", to = "begin"},
{name = "reset", from = "begin", to = "reset"},
},
callbacks = {
onenterbegin = function(self, event, from, to, msg)
warn("[Server Match Manager] Entered Begin State")
ServStorage.Events.ReplicateUpdate:Fire("MatchTable",{"Value","state","current"}, instance.state.current)
StageManager:TeleportToStage(player1,player2)
end,
onenterreset = function(self, event, from, to, msg)
ServStorage.Events.ReplicateUpdate:Fire("MatchTable",{"Value","state","current"}, instance.state.current)
warn("[Server Match Manager] Entered Reset State")
end,
}
})
warn("[Server Match Manager] Successfully created a new match")
return setmetatable(instance,SMM)
end