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

Datastore won't load ?

Asked by 8 years ago

I'm following a tutorial I found in the RBLX forums but for some reason I can't get the save to load? Can anyone show me what i'm doing wrong and and a method on how to correct it?

Also I was wondering what are some ways i can switch from the unreliable "Save on Remove" to a system that saves every 60 seconds?

saveAs = "LS"

dataStore = game:GetService("DataStoreService"):GetDataStore(saveAs)

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() --To load stuff
wait(1) --In case of crash and prevent being homeless
stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
for i = 1, #stats do --Creates a loop for every stats
dataStore:SetAsync(stats[i].Name, stats[i].Value .. player.userId)
end
end)

game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady() --To load stuff
wait(1) --In case of crash and prevent being homeless
stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
for i = 1, #stats do --Creates a loop for every stats
stats[i].Value = dataStore:GetAsync(stats[i].Name .. player.userId)
end
end)

1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

On line 10, your key looks like it's misplaced:

saveAs = "LS"

dataStore = game:GetService("DataStoreService"):GetDataStore(saveAs)

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() --To load stuff
wait(1) --In case of crash and prevent being homeless
stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
for i = 1, #stats do --Creates a loop for every stats
dataStore:SetAsync(stats[i].Name .. player.userId, stats[i].Value)
end
end)

game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady() --To load stuff
wait(1) --In case of crash and prevent being homeless
stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
for i = 1, #stats do --Creates a loop for every stats
stats[i].Value = dataStore:GetAsync(stats[i].Name .. player.userId)
end
end)

As for saving every 60 seconds, just set up a loop that runs and saves the data of every player every minute.

saveAs = "LS"

dataStore = game:GetService("DataStoreService"):GetDataStore(saveAs)

game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady() --To load stuff
wait(1) --In case of crash and prevent being homeless
stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
for i = 1, #stats do --Creates a loop for every stats
stats[i].Value = dataStore:GetAsync(stats[i].Name .. player.userId)
end
end)

while wait(60) do
    for _, player in pairs(game.Players:GetChildren() do
        stats = player["leaderstats"]:GetChildren() --Change "leaderstats" to your stats
        for i = 1, #stats do --Creates a loop for every stats
            dataStore:SetAsync(stats[i].Name .. player.userId, stats[i].Value)
        end
    end
end
0
Ohhhhhh I see what I did wrong. Thank you for your help sir. PsychonautX 15 — 8y
0
No problem. Pyrondon 2089 — 8y
Ad

Answer this question