So recently, I have been working with Data Stores, and trying to save stuff. Unfortunately, there is a problem.
I don’t get why these errors occur, am I doing something wrong?
Here is the code:
local dataStoreService = game:GetService("DataStoreService")
local testDatastore = dataStoreService:GetDataStore("dataSwordTest")
local testString = "dataSwordString"
local swordNumber
local sworditems
local defaultNumber = 0
local globalChar
game.Players.PlayerAdded:Connect(function(plr)
if plr then
local chr = plr.Character or plr.CharacterAdded:Wait()
globalChar = chr
sworditems = chr:WaitForChild("sworditem(s)")
swordNumber = sworditems:WaitForChild("swrdNumber")
local data
local success, fail = pcall(function()
data = testDatastore:GetAsync(testString)
end)
if success and data then
print("Loaded sword data.")
swordNumber.Value = data
else
print("Failed to load data.")
swordNumber.Value = defaultNumber
end
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
local success, response = pcall(function()
testDatastore:SetAsync(testString, swordNumber.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end
end)
Im just lost right now. The issue is, the Data Store is having problems with loading and saving the value, and I don’t seem to know why. Any help is appreciated.
You need to use game.Players.PlayerRemoving with game:BindToClose()
:BindToClose() is used when the server shuts down for x,y,z reasons. It is not used, when a player leaves, if the server is still Active. :BindToClose only fires once per server, right before it shutting down . However, if the server shuts down, sometimes Players.PlayerRemoving never fires! Meaning data loss. So its important you don’t use one or the other, you use both together .
Also you need a unique key to save and load data with a player. A unique key allows different users to save their data to the same datastore. You can fix this with using the UserID of a player:
Notice, I am trying to save a value that is inside of the character instead of the player, could that be why?
Or could I be that I am changing the value from the character inside of a LocalScript? idk.
local dataStoreService = game:GetService("DataStoreService")
local testDatastore = dataStoreService:GetDataStore("dataSwordTest")
local testString = "dataSwordString"
local swordNumber
local sworditems
local defaultNumber = 0
local globalChar
game.Players.PlayerAdded:Connect(function(plr)
if plr then
local chr = plr.Character or plr.CharacterAdded:Wait()
globalChar = chr
sworditems = chr:WaitForChild("sworditem(s)")
swordNumber = sworditems:WaitForChild("swrdNumber")
local data
local success, fail = pcall(function()
data = testDatastore:GetAsync(testString .. plr.UserId)
end)
if success and data then
print("Loaded sword data.")
swordNumber.Value = data
else
print("Failed to load data.")
swordNumber.Value = defaultNumber
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, response = pcall(function()
testDatastore:SetAsync(testString .. player.UserId, swordNumber.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
local success, response = pcall(function()
testDatastore:SetAsync(testString .. player.UserId, swordNumber.Value)
end)
if success then
print("data saved")
else
warn("Error.")
end
end
end)
The script is also located in ServerScriptService, so should I move it?