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

(CLOSED) How to fix this Data Store? [closed]

Asked by 9 years ago

I need help with a script with data stores, I want to correct the mistakes because when I tested it out the only thing this has done was remove my stats. I want it to actually save it then load it back again when the player leaves and comes back.

local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
print(stats[i].Name)
datastore:SetAsync(stats[i].Name, stats[i].Value)
end
end)

-- Loading dem stats
game.Players.PlayerAdded:connect(function(newplayer)
newplayer:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
wait(2) -- just in case
local stats2 = newplayer:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name)
end
end)

The script below is the script for making the leaderstats. NOTE THE TWO CODES ARE IN DIFFERENT SCRIPTS!

game.Players.PlayerAdded:connect (function(plr)
    local leaderstats = Instance.new("Model",plr)
    leaderstats.Name = "leaderstats"

    local lvl = Instance.new("IntValue", leaderstats)
    lvl.Name = "LVL"
    lvl.Value = 1

    local currency = Instance.new("IntValue", leaderstats)
    currency.Name = "Gold"
    currency.Value = 100

end)

0
Maybe you need to remove the wait(2). I've never seen that before in a data store before. LordSpectrus 25 — 9y
0
I asked a similar question yesterday, never got a response. Good luck :/ Antharaziia 75 — 9y

Locked by woodengop, HungryJaffer, and Spongocardo

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

1 answer

Log in to vote
0
Answered by 9 years ago

I have found out through Roblox Forum, for those who also got stuck on this here!

-- Data Store Script

local datastore = game:GetService("DataStoreService"):GetDataStore("PlayerStats")

-- Loading
game.Players.PlayerAdded:connect(function(player)
local id = player.userId
player:WaitForDataReady() 
local stats2 = player:WaitForChild("leaderstats"):GetChildren()

for i = 1, #stats2 do
stats2[i].Value = datastore:GetAsync(stats2[i].Name..id)
end
end)

-- Saving
game.Players.PlayerRemoving:connect(function(player)
local id = player.userId
--player:WaitForDataReady() -- Don't need it, cause of not saving
local stats = player:FindFirstChild("leaderstats"):GetChildren()

for i = 1, #stats do -- creates a loop for all the stats
datastore:SetAsync(stats[i].Name..id, stats[i].Value)
end
end)

I hope for those people who also got stuck with data stores finally got it!

Ad