So my dilemma is that I have this simple data script that is used for saving player data. I have no idea what is wrong with this. It’s not saving anything.
Code:
local DSS = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local playerData = DSS:GetDataStore("testData1")
local baseData = {
wins = 0;
coins = 0;
kills = 0;
claimed = 0;
reset = false
}
function checkTableEquality(t1,t2)
for i,v in next, t1 do if t2[i]~=v then return false end end
for i,v in next, t2 do if t1[i]~=v then return false end end
return true
end
Players.PlayerAdded:Connect(function(player)
-- creating stats
local leaderstats = Instance.new("Folder")
leaderstats.Archivable = false
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local generalstats = Instance.new("Folder")
generalstats.Archivable = false
generalstats.Name = "generalstats"
generalstats.Parent = player
-- creating values
local Vwins = Instance.new("IntValue")
Vwins.Name = "Wins"
Vwins.Parent = leaderstats
local Vcoins = Instance.new("IntValue")
Vcoins.Name = "Coins"
Vcoins.Parent = generalstats
local Vkills = Instance.new("IntValue")
Vkills.Name = "Kills"
Vkills.Parent = generalstats
local Vclaimed = Instance.new("IntValue")
Vclaimed.Name = "Claimed"
Vclaimed.Parent = generalstats
local Vreset = Instance.new("BoolValue")
Vreset.Name = "Reset"
Vreset.Parent = generalstats
local Vwins2 = Vwins:Clone()
Vwins2.Parent = generalstats
-- getting player data
local currentData
local fail, result = pcall(function()
currentData = playerData:GetAsync("dataKey-"..tostring(player.UserId))
end)
-- checking if they have any saved data
if result then
Vwins.Value = currentData.wins
Vwins2.Value = currentData.wins
Vcoins.Value = currentData.coins
Vkills.Value = currentData.kills
Vclaimed.Value = currentData.claimed
Vreset.Value = currentData.reset
else
playerData:SetAsync("dataKey-"..tostring(player.UserId), baseData)
Vwins.Value = 0
Vwins2.Value = 0
Vcoins.Value = 0
Vkills.Value = 0
Vclaimed.Value = 0
Vreset.Value = false
end
--[[game:BindToClose(function()
local newData = {
wins = Vwins.Value;
coins = Vcoins.Value;
kills = Vkills.Value;
claimed = Vclaimed.Value;
reset = Vreset.Value
}
if checkTableEquality(playerData:GetAsync("dataKey-"..tostring(player.UserId)), newData) then return end
playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
end)]]
end)
Players.PlayerRemoving:Connect(function(player)
local Vwins = player.leaderstats.Wins
local Vcoins = player.generalstats.Coins
local Vkills = player.generalstats.Kills
local Vclaimed = player.generalstats.Claimed
local Vreset = player.generalstats.Reset
local newData = {
wins = Vwins.Value;
coins = Vcoins.Value;
kills = Vkills.Value;
claimed = Vclaimed.Value;
reset = Vreset.Value
}
playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
end)
_G.getPlayerData = function(player)
return playerData:GetAsync("dataKey-"..tostring(player.UserId))
end
_G.setPlayerData = function(player, newData)
local fail, result = pcall(function()
playerData:SetAsync("dataKey-"..tostring(player.UserId), newData)
end)
if result then
print(player.Name.."'s data was changed.")
else
print(player.Name.."'s data was not changed.")
end
end