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

Data Stores will not load data. How to fix?

Asked by 5 years ago

This is my code:

local DataStoreService = game:GetService("DataStoreService")
local PointsStore = DataStoreService:GetDataStore("BBSystems", "PointsValueHolder")

game.Players.PlayerAdded:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    local leaderstats = Instance.new("Folder")
    local Points = Instance.new("NumberValue")
    local Rank = Instance.new("StringValue")

    leaderstats.Parent = Player
    leaderstats.Name = "leaderstats"

    Points.Parent = leaderstats
    Points.Name = "Points"
    Points.Value = PointsStore:GetAsync(Key)

    Rank.Parent = leaderstats
    Rank.Name = "Rank"
    Rank.Value = Player:GetRoleInGroup(4494167)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    PointsStore:SetAsync(Key, Player.leaderstats.Points.Value)
end)

and for some reason It will not load the data up when the play joins (Im not sure if it saves it either)

If you could help, that would be much appreciated!

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

try using a pcall to get the data and set it to a variable. this always works for me

local aDataStore = game:GetService("DataStoreService"):GetDataStore("aDataStore")

game.Players.PlayerAdded:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    local leaderstats = Instance.new("Folder")
    local Points = Instance.new("NumberValue")
    local Rank = Instance.new("StringValue")

    leaderstats.Parent = Player
    leaderstats.Name = "leaderstats"

    Points.Parent = leaderstats
    Points.Name = "Points"
    Points.Value = aDataStore:GetAsync(Key)

    Rank.Parent = leaderstats
    Rank.Name = "Rank"
    Rank.Value = Player:GetRoleInGroup(4494167)

    local player_data_points

    pcall(function()
        player_data_points = aDataStore:GetAsync(Key)  
    end)

    if player_data_points ~= nil then
        Player.leaderstats.Points.Value = player_data_points
    end

end)

game.Players.PlayerRemoving:Connect(function(Player)
    local Key = "uid_" .. Player.UserId
    aDataStore:SetAsync(Key, Player.leaderstats.Points.Value)
end)
0
Doesn't work.. ItsDodgee 0 — 5y
0
It works for me. here, I'll edit it to show the final code. you need to join an actual game by the way- data won't save in studio. I actually did change a few things but it works- saves points and gets the players current group rank. accept my answer if everything works :) Spiritlotus 151 — 5y
0
Doesn't work.. Where is your script placed? ItsDodgee 0 — 5y
0
a server script under ServerScriptStorage Spiritlotus 151 — 5y
View all comments (4 more)
0
Nothing ItsDodgee 0 — 5y
0
I edited my answer, are you sure your doing everything? I have this very script under serverscriptstorage and it works. you must be doing something wrong. Spiritlotus 151 — 5y
0
I imputed the code into a script, put that script in serverscriptstorage but nothing ItsDodgee 0 — 5y
0
do you have discord? ill help koala#6590 i dont know why its not working for you. it works for me and it only saves IN GAME not in STUDIO Spiritlotus 151 — 5y
Ad

Answer this question