So, my DataStore script doesn’t work. I’m not sure if it’s having trouble saving or loading, but something’s wrong.
Yes, DataStoring in Studio is enabled.
I tried to see if it saved with the DataStore Editor plugin but I’m not so sure on how to use it. When it asked for a key, I was assuming it wanted the table index number.
oof
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Player_Data")
game.Players.PlayerAdded:connect(function(Player)
local PlayerData
local DataVars = Instance.new('Folder')
DataVars.Name = "PlayerData"
DataVars.Parent = Player
local Money = Instance.new('IntValue',DataVars)
Money.Name = "Money"
local XP = Instance.new('IntValue',DataVars)
XP.Name = "XP"
------
local CanSave = Instance.new('BoolValue',Player)
CanSave.Name = "CanSaveData"
CanSave.Value = true
local DataFetchSuccess, ErrorMessage = pcall(function()
PlayerData = DataStore:GetAsync(tostring(Player.UserId))
end)
if DataFetchSuccess then
if PlayerData ~= nil then
Money.Value = PlayerData[1]
XP.Value = PlayerData[2]
else
Money.Value = 50
XP.Value = 0
end
else
Player.CanSaveData.Value = false
Player:Kick("Your data didn't load! Please rejoin.")
end
end)
game.Players.PlayerRemoving:connect(function(Player)
if Player.CanSaveData.Value == false then return end
local PlayerData = {Player.PlayerData.Money.Value, Player.PlayerData.XP.Value,}
local DataWriteSuccess, ErrorMessage = pcall(function()
DataStore:SetAsync(tostring(Player.UserId), PlayerData)
end)
if not DataWriteSuccess then
local Retry_Count = 0
while Retry_Count < 6 do
wait(60)
local Succeeded,Error = pcall(function()
DataStore:SetAsync(tostring(Player.UserId), PlayerData)
end)
if Succeeded then break end
Retry_Count = Retry_Count+1
end
end
end)
You should put print statements at each of the important parts and then see what doesn’t print but if everything prints you are most likely saving it wrong
I found out that the money variable doesn’t save but the xp variable does… I put both of them in the table in order. At first, I thought maybe it worked like other languages did, with the 1st table entry always being 0. I tried making the 1s 2s and the 2s 3s but it didn’t work at all. I’m confused… oof
The way you’re doing it is wrong, DataFetchSuccess would be nil because you’re using it inside of the pcall when no value is returned yet and thus it’s still nil.
What is the money supposed to be at? and how are you changing it when you test. The values need to be changed serverside if you are doing it manually or else the script wont see the change
do a print(Player.PlayerData.Money.Value) right before setAsync. Sometimes when you quit testing mode the player does not get removed before the server shuts down. Often times you gotta manually delete the player from game.Players to get the Player removing event to fire.