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

How could i fix this?

Asked by 10 years ago

This is a datastore script for leaderstats located in the workspace in a normal script. How could i make this more stable? If i'm doing something wrong, i tried it out sometimes (SOMETIMES) it does not save or load right. Am i doing something wrong, how could i fix it?

Game.Players.PlayerAdded:connect(function(Player)
Player:WaitForChild("leaderstats")
DataStore = Game:GetService("DataStoreService"):GetDataStore(Player.Name.."Stats")
StorageEnter = Player.leaderstats:GetChildren()
for i = 1, #StorageEnter do 
StorageEnter[i].Value = DataStore:GetAsync(StorageEnter[i].Name)
end
end)
Game.Players.PlayerRemoving:connect(function(Player)
DataStore = Game:GetService("DataStoreService"):GetDataStore(Player.Name.."Stats")
StorageLeave = Player.leaderstats:GetChildren()
for i = 1, #StorageLeave do
StorageLeave[i].Value = DataStore:SetAsync(StorageLeave[i].Name, StorageLeave[i].Value)
end
end)

1 answer

Log in to vote
-1
Answered by
istiyak 13
10 years ago

This is not my script but

-- Read the description of the model for details, it pretty much says to just put it in.


function onPlayerEntered(player)
wait()-- Change to wait for player longer.
player:WaitForDataReady()
repeat wait() until player:FindFirstChild("leaderstats")
if player.DataReady then
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
local ScoreLoaded = player:LoadNumber(score[i].Name)
wait()
if ScoreLoaded ~= 0 then
score[i].Value = ScoreLoaded
end
end
end
end
end

function onPlayerLeaving(player)
if player:findFirstChild("leaderstats") then
local score = player.leaderstats:GetChildren()
for i = 1,#score do
player:SaveNumber(score[i].Name,score[i].Value)
end
end
end

game.Players.PlayerAdded:connect(onPlayerEntered)
game.Players.PlayerRemoving:connect(onPlayerLeaving)

if this is a gui, im not the expert, soz :/

Ad

Answer this question