I am working on making a magic system into my game. To gain magics you open crates, when you open a crate it creates a value named after the magic you earned (if it is not created already). If you already own one, it increases the value by one. My plan is to make it so the value saves and when you join, it can put it in your inventory. The problem is that when I try to just save the value itself, it doesn't save.
The script I have:
local DataStore = game:GetService("DataStoreService"):GetDataStore("PlayerData")
game.Players.PlayerAdded:connect(function(plr)
local key = "key-" .. plr.UserId
local Magics = Instance.new("Folder",plr)
Magics.Name = "Magics"
pcall(function()
save = DataStore:GetAsync(key)
end)
if save then
for i,v in pairs(save) do
-- where the inventory would be set up
end
else
local load = plr.Magics:GetChildren()
DataStore:SetAsync(key, load)
end
end)
game.Players.PlayerRemoving:connect(function(plr)
local key = "key-" .. plr.UserId
local load = {}
for i,v in pairs(plr.Magics:GetChildren()) do
table.insert(load,v.Value)
end
DataStore:SetAsync(key, load)
end)
Am I trying to do the right thing, or is there a more efficient way of doing this?
If you help, thank you!

1 comment
You are amazing!