#How can I Improve my datastore here?

45 messages · Page 1 of 1 (latest)

violet pelican
#

Hey everyone, with my current Datastore it works but it appears everything I leave the game I keep getting that datastore saving queue issues, and with me adding new things into the game I keep adding new data for bools or number values etc so I'm just wondering if there is a better way how I can code this. (I know it may look like really bad coding but it does the job)

dawn storm
violet pelican
#

Basically everything in here holds all the stats and data for the player

#

For example when they leave it saves their clicks value and when they join it gives it back to them

dawn storm
#
local Player = game.Players
Player.PlayerAdded:Connect(function(player)
    local Folders = {
        {key = 'leaderstats'},{key = 'MainFolder'},{key = 'Stats'},{key = 'CostFolder'}
    }
    for i,v in ipairs(Folders) do -- add "in ipairs(Folders)" here
        local Folder = Instance.new('Folder', player)
        Folder.Name = v.key
        print(Folder.Name)
    end
end)```
#

why

#

but this is what it should look like for the Folder Adding

violet pelican
#

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

This also works it's just another way of doing it

merry sequoia
#

You can store all the values in one DataStore as a table

dawn storm
#

na that way is kinda not good

#

leaderstats.Parent = plr

#

its better to dolua local ls = Instance.new('Folder',player)

merry sequoia
#

Its good both ways

#

Both ways work the same

#

The only difference is that one has one more line

#

Also, why you need the data store?

#

I'm you can make it in under 50 lines

dawn storm
#

ye dont listen to AVG since doing a table to handle the instances of the Folders Name

merry sequoia
#

Wtf do you mean?

violet pelican
dawn storm
violet pelican
#

That might be why I'm getting the queuing error then

merry sequoia
#

I dont think you get what I mean:
You create a empty table, loop thru every stat you want to save and add the stat.Name and stat.Value inside the table you have created. After the loop is done, just save the table
When a player joins, check if he was data and if it does, loop thru stats and load data from the table to .Value using the .Name to get the specific data

dawn storm
#

so its better to dolua function UpdateData(player) local Update = {} local ls = player:FindFirstChild('leaderstats'):GetChildren() local MainFolder = player:FindFirstChild('MainFolder'):GetChildren() local CostFolder = player:FindFirstChild('CostFolder'):GetChildren() for i,v in pairs(ls) do Update[v.Name] = v.Value end for i,v in pairs(MainFolder) do Update[v.Name] = v.Value end for i,v in pairs(CostFolder) do Update[v.Name] = v.Value end return Update end game.Players.PlayerRemoving:Connect(function(player) local ToUpdateData = UpdateData(player) DataStore:SetAsync(player.UserId,ToUpdateData) end)

merry sequoia
merry sequoia
violet pelican
#

I'll set that up and change it all

dawn storm
#

ye its much better than the others

#

k

#

also u can do that for the GetAsync

violet pelican
#

Thanks both of you

dawn storm
#
for i,v in pairs(ls:GetChildren()) do
       if ToSave then
            v.Value = ToSave[v.Name]
      end
end```
merry sequoia
#

I would recommend you to create a folder where you have every stat you need (Call it DataFolder or smth like that) and don't have anything inside a leaderstats folder that need to be saved

dawn storm
#

far better thanlua if ToSave then Cost.Value = ToSave.Cost Points.Value = ToSave.Points Money.Value = ToSave.Money end

willow mauve
#

You can use a library

#

Like profile service / data store 2

dawn storm
#

yes also that

#

lol

merry sequoia
#

Here is an example of how you can do it (you could improve it in many ways, but this is just perfect for someone who just start scripting)

dawn storm
merry sequoia
#

Half the lines are just comments to explain whats going on

#

And hard to read because is a txt file, if you take it inside of a Roblox Script, you can read it

dawn storm
#

now this is what its should look like trust me