Hi. am trying to use Data Store for the first time. Am having a hard time figuring out why this script dos not save my leaderstats when leaving a local test.. This script is not in a local script and I've unchecked the API box in game configuration. Any help would be appreciated :).
Da code:
local DS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local DSStats = DS:GetDataStore("Stats")
--local datastore = DS:GetDataStore("PlayerStats")
game.Players.PlayerAdded:connect(function(player)
--player:WaitForDataReady() -- make sure we aren't looking for leaderstats before they are created
--wait(2) -- just in case
--creating leaderstats
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local goldstats = Instance.new("IntValue",leaderstats)
goldstats.Name = "Gold"
------------------------
local data = DSStats:GetAsync(tostring(player.UserId))
if data==nil then
print("data is nil")
return
end
---exstract data
local PlayerStats = player:FindFirstChild("leaderstats"):GetChildren()
for i=1,#PlayerStats do
PlayerStats[i].Value=data[PlayerStats[i].Name] or 0
print(PlayerStats[i])
end
end)
game.Players.PlayerRemoving:connect(function(player)
local Data={}
local PlayerStats = player:FindFirstChild("leaderstats"):GetChildren()
for i = 1, #PlayerStats do -- creates a loop for all the stats
Data[PlayerStats[i].Name]=PlayerStats[i].Value
print(PlayerStats[i].Value)
end
DSStats:SetAsync(tostring(player.UserId), Data)
end)
(script is in ServerScriptService)

2 comments
O ok i have only tested it in local test. Ill test it in server test when i get home. Thank you for your knowledge :)
worked thanks!