So far I have this system where it updates the leaderstat when the player touches a pad but not when they go back to a pad, the updating is done and the api is enabled. My question is, why is my datastore not saving? It keeps saying it’s 1 even when I set my value higher than that.
local ds = game:GetService("DataStoreService"):GetDataStore("Levels")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstat = Instance.new("Folder")
leaderstat.Name = "leaderstats"
leaderstat.Parent = plr
local level = Instance.new("NumberValue")
level.Name = "Levels"
local success, dsValue = pcall(function()
return ds:GetAsync(plr.Name)
end)
--I suspect its happening in this if statement
if success then
if dsValue then
level.Value = dsValue
else
level.Value = 1
end
end
level.Parent = leaderstat
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync(plr.Name, plr.leaderstats.Levels.Value)
end)
Did you set it on the server or on the client? Also, I would not recommend saving the DataStore based on the player’s name, as this will result in the player losing progress if they change their name.
try printing the value of levels before your if statement. The pcall() function returns success, err when the function errors, so you should get an error message if you print your levels variable.
Try this. Also you either need to kick yourself or you should test it in the actual game, because Studio datastore’s don’t work as well:
local ds = game:GetService("DataStoreService"):GetDataStore("Levels")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstat = Instance.new("Folder")
leaderstat.Name = "leaderstats"
leaderstat.Parent = plr
local level = Instance.new("NumberValue")
level.Name = "Levels"
local data = false
local success, dsValue = pcall(function()
data = ds:GetAsync(plr.Name)
end)
--I suspect its happening in this if statement
if success and data then
level.Value = data
else
level.Value = 1
end
level.Parent = leaderstat
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync(plr.Name, plr.leaderstats.Levels.Value)
end)
I did it a bit differently than you but this is how I implemented it for anyone wondering
game:BindToClose(function()
for i=1,#game.Players:GetPlayers() do
ds:SetAsync(game.Players:GetPlayers()[i].Name, game.Players:GetPlayers()[i].leaderstats.Levels.Value)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
ds:SetAsync(plr.Name, plr.leaderstats.Levels.Value)
end)