I need help with saving character's exp

So I’m trying to do an inventory system with summoning like All Star Tower Defense,
I’ve made it so it saves the characters you get but I don’t know how I would do it so it
creates a NumberValue and gives the value of exp it had when it saved.

I’ve looked for any solutions, haven’t found any.

So what I thought could work is to make a table where it stores the character’s name and the exp so when you join it knows where to add the exp.

Here’s how I save them
https://gyazo.com/3672121550f53524965d9e5dc2e45946

3 Likes

Arrays are the solution you seek for.

local toSave = {
   EXP = 2,
   idkWhat = {"tool1", "tool2"}
}
dataStore:SetAsync(plr,toSave)
2 Likes

So I would have to do, not full code, I’m just imagining this is it.

local toSave

game.Players.PlayerAdded:Connect(function(plr)
 local key = "id_"..plr.UserId
 local GetSaved = ds:GetAsync(key)
 if GetSaved then
  toSave =? or something like that?
 else
  print("New User")
 end
end)
1 Like

Then how would I be able to tell it who’s it’s parent name and the current exp it has?

1 Like

Something like

local data = {
    Goku = {
        XP = 125
    },
    ['Mori Jin'] = {
        XP = 125
    }
}
1 Like

But what if I have more than one with the same name?

1 Like
local Data = {}

for i, Character in pairs(Inventory:GetChildren()) do
	table.insert(Data, {
		Name = Character.Name,
		XP = Character.XP.Value
	})
end

The data table would look like this

{
	{
		Name = "Goku",
		XP = 125235
	},
	{
		Name = "Mori Jin",
		XP = 1252531
	},
	{
		Name = "Mori Jin",
		XP = 125235
	},
	{
		Name = "Saitama",
		XP = 125253235
	},
	{
		Name = "Vegeta",
		XP = 1255
	},
}
1 Like

Right I’ll try it out now thanks!

1 Like

What you’re trying to do with =? is the ternary operator.

In Lua it is used like this condition and ifTrue or ifFalse
for example

local bool = false
print(bool and "hey" or "hello") -- hello

You can shorthand it to ifTrue or ifFalse, meaning if the first one is false/nil then it assigns the second

So it would be something like this

local data = ds:GetAsync(key) or {} -- whichever default value you want
1 Like

No I meant what should I do with what you gave to load and save it no like actually =?
I know =? doesn’t exist

1 Like

So I did it and it works but it only loads 1 character.
So I’ve done the loading in for loops but it only does the last character in the array
I tried adding more for loops but nothing

Can you show how you load them?

2 Likes

Nevermind I fixed it I didn’t know why it didn’t work but I took a break to the bathroom and puff and an idea popped into my mind. I re-did the code and it works, the only thing left is to make it so it saves the exp because currently, it doesn’t save it.

Thank you so much I should have thought about arrays.

I sent a friend request on dc so we can talk more anyway thanks! :relaxed: