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

DataStore not saving the strength. Are the datastores outdated?

So i'm currently making a lift sim but i could not get the datastore working. I didn't find any errors in the code so i need help. Here is the code:

local ds = game:GetService("DataStoreService")
local StrengthData = ds:GetOrderedDataStore("Strength")

game.Players.PlayerAdded:Connect(function(player)
	local getSavedStrength = StrengthData:GetAsync(player.UserId)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"
	local strength = Instance.new("IntValue",leaderstats)
	strength.Name = "Strength"
	strength.Value = getSavedStrength
end)


game.Players.PlayerRemoving:Connect(function(player) -- i think the problem is in this function
	local strengthValue = player.leaderstats.Strength
	StrengthData:SetAsync(player.UserId,strengthValue)
	print("saved data") 
end)

When i was testing if the datastore works, it didnt even print "saved data" which is in the code. and btw the console doesnt print any errors

1 answer

Several issues, the first issue is that you should wrap the script in a pcall a pcall is basically something that protects you from getting errors.

Second Issue is that you forgot to set the Strength equal to the Data let me do it for you

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

wait(2)

game.Players.PlayerAdded:Connect(function(player)
 local leaderstats = Instance.new("Folder")
 leaderstats.Name = "leaderstats"
 leaderstats.Parent = player
 
 local Strength = Instance.new("IntValue")
 Strength.Name = "Strength"
    Strength.Parent =  leaderstats

    if player.Character.Tool.Activated then
 Strength.Value = Strength.Value + 1
 
 local PlayerUserId = "Player_"..player.UserId
 
 local Data
 local sucess, errormessage = pcall(function()
  local Data = myDataStore:GetAsync(PlayerUserId)
 end)
 
 if sucess then
  Strength.Value = Data
 end
 
 
 
end

game.Players.PlayerRemoving:Connect(function(player)
 
 local PlayerUserId = "Player_"..player.UserId
 
 local Data = player.leaderstats.Strength.Value
 
 local sucess, errormessage = pcall(function()
  myDataStore:SetAsync(PlayerUserId, Data)
 end)
 
 if sucess then
  print("Data succesfully saved")
 else
  print("Data not saved")
  warn(errormessage)
 end
end)
end)


Remember to turn on API Services. And the DataStore Does not work in Studio it only works in the actual ROBLOX

Please consider commenting if there are any questions or errors.

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