Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Why does this data saving script not work at all?

Asked by 6 years ago

This script is supposed to create and save a subset of player stats that is supposed to be saved and loaded upon the player joining and leaving respectively.

When I increase/change one of the data points and leave and rejoin, the data does not load. This script is currently being tested on a private game, if that matters at all.

What is the issue with it? Output gave me nothing to work with.

01local DataStores = game:GetService("DataStoreService")
02local PlayersDataStore = DataStores:GetDataStore("Players")
03local FindValues = {
04"Rank","XP","Prestige","Heists";
05--Add any new variable!
06}
07local TypeValues = {
08    Rank = "IntValue"; --Or int whatever is fine...
09    XP = "IntValue";
10    Prestige = "IntValue";
11    Heists = "IntValue";
12}
13 
14function SetupPlayer(Player)
15local LeaderStats = Instance.new("Folder",Player)
View all 52 lines...

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 years ago

There are a few issues with your script:

1) It is difficult to read, create separate functions for each task so it can be more organized (save and load data function).

2) The tables are unnecessary and it was hard to understand it. I suggest to create a variable for each value. It'll be more organized and efficient.

3) 'connect' is deprecated, use 'Connect' instead.

4) DO NOT set the parent in the Instance.new parameters. It's a faster way but it was proved that it can slow down the script.

01local Players = game:GetService("Players")
02local DataStores = game:GetService("DataStoreService")
03local PlayersDataStore = DataStores:GetDataStore("Players")
04 
05local function SavePlayerData(Player)
06    if Player then
07        local Leaderstats = Player:FindFirstChild("perm")
08        if Leaderstats then
09            local XPValue, RankValue, HeistsValue, PrestigeValue
10            XPValue = Leaderstats:FindFirstChild("XP")
11            RankValue = Leaderstats:FindFirstChild("Rank")
12            HeistsValue = Leaderstats:FindFirstChild("Heists")
13            PrestigeValue = Leaderstats:FindFirstChild("Prestige")
14            if XPValue and RankValue and HeistsValue and PrestigeValue then
15                local PlayerDS = PlayersDataStore:GetAsync(Player.UserId)
View all 83 lines...
0
Don't forget to add a PlayerRemoving event so you can save players data when they leave the game. awesomeipod 607 — 6y
0
Make sure studio access to API services is enabled as well awesomeipod 607 — 6y
0
PlayerDataStore:SetAsync(Player.UserId, SavingTable) is undefined in the script? CoolJohnnyboy 121 — 6y
0
They're in lines 24 42 awesomeipod 607 — 6y
View all comments (2 more)
0
Instead of SetAsync to update a player's data who already has data and isn't new, shouldn't you use UpdateAsync? User#22980 0 — 6y
0
Yes but they both have the same function awesomeipod 607 — 6y
Ad

Answer this question