So, I’ve been trying to figure out on how to save numbervalues to a data store. And I made a script but it’s not working, there are no errors in the output, and I have API service activated.
local DataStoreService = game:GetService("DataStoreService")
local DataStoreName = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local levels = Instance.new("Folder")
levels.Name = "Levels"
levels.Parent = player
local Level1 = Instance.new("NumberValue")
Level1.Name = "Level1"
Level1.Parent = levels
local data
local success, errormessage = pcall(function()
data = DataStoreName:GetAsync(player.UserId.. "-Levels")
Level1.Value = data
end)
if success then
print("Data Loaded")
else
print("Error")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStoreName:UpdateAsync(player.UserId.."-Levels",function(old)
local newvalue1 = old
newvalue1 = player.Levels.Level1.Value
return newvalue1
end)
end)
if success then
print("Data has been saved")
else
print("Error")
end
end)
function SafeSave(Datastore, Key, Value)
local Attempts = 0
local Success = false
local Error = nil
repeat
Attempts += 1
Success, Error = pcall(function()
Datastore:SetAsync(Key, Value)
end)
if not Success then wait(10) end
until
Success or Attempts >= 3
if Error ~= nil or not Success then print("<Error while saving data>\n - "..(Error or "No Error")) return nil end
return Success
end
function SafeGet(Datastore, Key)
local Attempts = 0
local Data = nil
local Success = false
local Error = nil
repeat
Attempts += 1
Success, Error = pcall(function()
Data = Datastore:GetAsync(Key)
end)
if not Success then wait(10) end
until
Success or Attempts >= 3
if Error ~= nil or not Success then print("<Error while retrieving data>\n - "..(Error or "No Error")) return nil end
return Data
end
function SafeUpdate(Datastore, Key, Logic)
local Attempts = 0
local Data = nil
local Success = false
local Error = nil
repeat
Attempts += 1
Success, Error = pcall(function()
Data = Datastore:UpdateAsnyc(Key, Logic)
end)
if not Success then wait(10) end
until
Success or Attempts >= 3
if Error ~= nil or not Success then print("<Error while updating data>\n - "..(Error or "No Error")) return nil end
return Data
end
local LevelsDataStore = game:GetService("DataStoreService"):GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local levels = Instance.new("Folder")
levels.Name = "Levels"
levels.Parent = player
local Level1 = Instance.new("NumberValue")
Level1.Name = "Level1"
Level1.Parent = levels
Level1.Value = SafeGet(LevelsDataStore, tostring(player.UserId).."-Levels") or 0
end)
game.Players.PlayerRemoving:Connect(function(player)
SafeSave(LevelsDataStore, tostring(player.UserId).."-Levels", player.Levels.Level1.Value)
end)
You have to put the parentheses after the player.Levels.Level1.Value because you are passing arguments to the function I presume. Tell me if it is still underlined.
Why is it working for you but not me? Cause I just went into a different place, copied and pasted the code, made a server script, and turned on API service, and it doesn’t save when I change the value.