Hello. Ive currently been trying to come up with a way to make a creature capturing game, like pokemon. However I cant understand how id go about saving the player data. The creatures would all have different stats, like for example: HP = X, ISPURPLE = Y, DEFENSE = Z. Id tought of storing them in Folders, however I do not know how I could go about saving this information. Any help is appreciated!
4 Likes
That’s generally a bad way of going about it. There isnt really a way to directly “save” instances (such as folders) either. What you’d do is save a table of information, and put tables with creatures full of their stats in that table.
2 Likes
By doing it that way, how would i access the player’s pokemon in other scripts?
You can loop through tables using
local table = {}
for i, v in pairs(table) do
end
As for your saving system, youll want to save each “pokemons” stats into a table within a table.
example code:
local PokemonData = {}
for i, v in pairs(Player.Party:GetChildren()) do
local NewPokemon = {} -- create a new table for each pokemon
NewPokemon.PokemonName = v.Name -- Create a new value in the table that is the pokemons name (for loading pokemon)
for i, c in pairs(v:GetChildren()) do -- Loop through the pokemons stats: ATK, DEF etc.
NewPokemon[c.Name] = c.Value -- this will create a value in the table like: {["ATK"] = 20}
end
table.insert(PokemonData, NewPokemon) -- Add the new pokemon to the table were going to save
end
For loading the pokemon, you can just create a folder, and then load the data.
example code:
local PokemonFolder = instance.new("Folder", Player)
PokemonFolder.Name = "Party"
for i, v in pairs(PlayerPokemonData) do
local NewPokemon = instance.new("Folder", PokemonFolder)
NewPokemon.Name = v.PokemonName
-- repeat this for each stat
local atk = instance.new("NumberValue", NewPokemon)
atk.Name = "ATK"
atk.Value = v.ATK
end
That way, you can access the players pokemon through other scripts, and keep your game generally the same.
2 Likes
Thank you! I think I understand, will try this later!
function saveprogress(location)
local load = {}
for i,v in pairs(location:GetChildren()) do
table.insert(load,v:Clone())
end
return load;
end
function loadprogress(load,location)
for i,v in pairs(load) do
v.Parent = location
end
end
example
local dss = game:GetService("DataStoreService")
local data = dss:GetDataStore("example")
function saveprogress(location)
local load = {}
for i,v in pairs(location:GetChildren()) do
table.insert(load,v:Clone())
end
return load;
end
function loadprogress(load,location)
for i,v in pairs(load) do
v.Parent = location
end
end
game.Players.PlayerAdded:Connect(function(p)
local motherfolder = Instance.New("Folder",p)
motherfolder.Name = "party"
local key = "key-"..p.UserId
local save = data:GetAsync(key)
if (save == nil) then
local poke = Instance.New("Folder",motherfolder)
local statExample = Instance.New("StringValue",poke)
poke.Name = "pokeplaceholder"
statExample.Name = "name"
data:SetAsync(key,saveprogress(motherfolder))
else
loadprogress(save,motherfolder)
end
end)
game.Player.PlayerRemoving:Connect(function(p)
local key, motherfolder = "key-".. p.UserId ,p:WaitForChild("party")
data:SetAsync(key,saveprogress(motherfolder))
end)