Review | Handling the data for a game with character selection

Hello everyone, I’m trying to create a character selection in my game and to handle the data with datastore2. I came to this conclusion after som research. Is this a good way to go about things ?

local DataStore2 = require(1936396537)

DataStore2.Combine("DATA", "Slots")
DataStore2.Combine("Slots", "Slot1")
DataStore2.Combine("Slots", "Slot2")
DataStore2.Combine("Slots", "Slot3")
DataStore2.Combine("Slots", "Settings")
DataStore2.Combine("Slots", "Slot")
DataStore2.Combine("Slot", "Exp")
DataStore2.Combine("Slot", "Gold")
DataStore2.Combine("Slot", "Inventory")

game.Players.PlayerAdded:Connect(function(player)
    local SlotsStore = DataStore2("Slots", player)
	local DataSlots = SlotsStore:GetTable({
		Slot1 = {Exp = 0, Gold = 0, Inventory = {false}}, 
		Slot2 = {Exp = 0, Gold = 0, Inventory = {false}}, 
		Slot3 = {Exp = 0, Gold = 0, Inventory = {false}}, 
		Settings = {Music = false, Trade = false, Slot = 0, World = 0}
	})
	
	if DataSlots.Settings.Slot == 0 then -- this mean the player wasn't here before so he needs to choose his character
	else -- this mean the player was here before and he was playing with this charatcer, putting it to slot so its easier to change stuff and i don't need to check everytime who he is
		DataSlots.Slot = DataSlots["Slot"..DataSlots.Settings.Slot]
	end
	
	SlotsStore:Set(DataSlots) -- putting in the cache the data
	
	SlotsStore:BeforeSave(function(DataSlots) -- and here, before saving the data, I switch back slot and the real slot of the player
		
		if DataSlots.Settings.Slot ~= 0 then
			DataSlots["Slot"..DataSlots.Settings.Slot] = DataSlots.Slot
			DataSlots.Settings.Slot = 0
		end
	
	    return DataSlots
	end)
end)

Slot1, 2 and 3 are used to store the data of the character1, 2 and 3. I’m using Slot, so in game I won’t need to check with which character they are playing (so I won’t need to do something like “Exp”…Slot). And before saving the data, I put back in place the data from slot to the character which was used.

2 Likes

You have a very good script here!
I would recommend, however, to put in a portion of code that automatically uses a template slot to create two more “copied” slots, in order to reduce repetitive code.

2 Likes

So something like this ?

local DataStore2 = require(1936396537)

DataStore2.Combine("DATA", "Slots")
DataStore2.Combine("Slots", "Slot1")
DataStore2.Combine("Slots", "Slot2")
DataStore2.Combine("Slots", "Slot3")
DataStore2.Combine("Slots", "Settings")
DataStore2.Combine("Slots", "Slot")
DataStore2.Combine("Slot", "Exp")
DataStore2.Combine("Slot", "Gold")
DataStore2.Combine("Slot", "Inventory")

local TempSlot = {Exp = 0, Gold = 0, Inventory = {false}}

game.Players.PlayerAdded:Connect(function(player)
    local SlotsStore = DataStore2("Slots", player)
	local DataSlots = SlotsStore:GetTable({
		Slot1 = TempSlot , 
		Slot2 = TempSlot , 
		Slot3 = TempSlot , 
		Settings = {Music = false, Trade = false, Slot = 0, World = 0}
	})
	
	if DataSlots.Settings.Slot == 0 then -- this mean the player wasn't here before so he needs to choose his character
	else -- this mean the player was here before and he was playing with this charatcer, putting it to slot so its easier to change stuff and i don't need to check everytime who he is
		DataSlots.Slot = DataSlots["Slot"..DataSlots.Settings.Slot]
	end
	
	SlotsStore:Set(DataSlots) -- putting in the cache the data
	
	SlotsStore:BeforeSave(function(DataSlots) -- and here, before saving the data, I switch back slot and the real slot of the player
		
		if DataSlots.Settings.Slot ~= 0 then
			DataSlots["Slot"..DataSlots.Settings.Slot] = DataSlots.Slot
			DataSlots.Settings.Slot = 0
		end
	
	    return DataSlots
	end)
end)
1 Like

yeeee. nice job.
30 characters? why tho?

2 Likes