Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Data Store Problem? [Un-Solved]

Someone helped me with a DataStore that should automatically save and load and it appears to do nothing with no error.

Game.Players.PlayerRemoving:connect(function(Player1)
local key = "user_" .. Player1.userId
Game:GetService("DataStoreService"):GetDataStore("Stats"):UpdateAsync(key, function(oldValue)
local Data1 = Player1:WaitForChild("leaderstats"):GetChildren()
for i= 1,#Data1 do
Data1:SetAsync(Data1[i].Name, Data1[i].Value) 
end
end)
end)

Game.Players.PlayerRemoving:connect(function(Player2)
local key = "user_"..Player2.userId
if Game:GetService("DataStoreService"):GetDataStore("Stats"):GetAsync(key) ~= nil then
local Data2 = Player2:WaitForChild("leaderstats"):GetChildren()
for i = 1, #Data2 do
Data2:GetAsync(Data2[i].Name)
end
end
end)

3 comments

0

Dude that's my answer from before, I can understand that you copy and pasted, q.q!

0

Ummm, i said someone helped me. (You). But it did not really work at all.

0

Well I notice a few mistakes so I can fix it , mk

1 answer

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

game.Players.PlayerRemoving:connect(function(player)
player:WaitForDataReady() 
wait(4)
local p1 = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #p1 do 
datastore:SetAsync(p1[i].Name, p1[i].Value)
end
end)

Simply this will save a players data.

game.Players.PlayerAdded:connect(function(nplayer)
nplayer:WaitForDataReady() 
wait(4) -- I added this on
local p2 = newplayer:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #p2 do
p2[i].Value = datastore:GetAsync(p2[i].Name)
end
end)

Hmm Try this, I mean this should work I made this one out of my game.

+1 if this worked and helped!!

6 comments

0

player:WaitForDataReady() is not needed. DataStore is always ready to be loaded.

0

Well try it.

0

I noticed you fire the function "nplayer" but in line 4 on the second player its newplayer witch is invalid.

0

Fixed it

0

This does not work.

0

Wierd.

This discussion is preserved as it was. New answers, comments, and votes are closed.