Data Module using GUID as Index

I am using a simple Data Module to store match information. There can be multiple matches running simultaneously on the server, some Solo, some 2 player, some 4 player. I need to track basic information such as the match type and which players are active in that match. This is a module I have used successfully previously.

The Data Module:

local Data = {}
local MatchData = {}

function Data.AddMatch(MatchID, Data) -- ADD a Match to the MatchData Table
	MatchData[MatchID] = Data
end

function Data.ReturnMatch(MatchID) -- RETRIEVE a Match's data from the MatchData Table 
	return MatchData[MatchID]
end

function Data.RemoveMatch(MatchID) -- DELETE a Match from the MatchData Table
	MatchData[MatchID] = nil
end

return Data

There 2 separate scripts that utilise this module:

  1. The MatchControl script which creates new Matches, sends the Match list to Players and allows players to join other matches
  2. The MatchManager script, which places all joined players into a new Match and manages the interaction between server and players, match timing and round progress

A new match is added to the queue as follows:

local newMatch = {}
local ID = HttpService:GenerateGUID(true)  -- example {591da82d-834b-4989-a100-273c838a4a72}
newMatch.ID = ID
newMatch.Owner = player -- player Object
newMatch.MatchType = matchType
newMatch.MaxPlayers = matchTypes[matchType]	-- number 1 > 4
newMatch.PlayerCount = 1	-- number 1 > 4
newMatch.Player1 = player.UserId -- player Object
newMatch.InProgress = false  -- Bool
DataModule.AddMatch(newMatch.ID, newMatch)  -- Adds the above table to DataModule

The problem comes when using a GUID for each match as the Index for the data. When initially creating the Match, I can immediately retrieve it within the same function, but can’t seem to retrieve the match data again using the Index once set.

local data = DataModule.ReturnMatch(ID) -- Errors "attempt to call a nil value"

Is it not possible to use a GUID as am Index using this method? If I do it all locally using a single script and not the module, then I can store and retrieve the match data, but I wanted to have a separate script handling the match progression for simplicity.

Where am I going wrong? I have a feeling I must be missing something obvious, but I can’t tell what.

Sad to say that this was all just typos. My mistake.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.