I wrote a script which is supposed to save leaderstats and tools. However, the tools bit always returns nil. It prints the correct value when saving, but when loading the correct stringValue, its always nil.
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PrisonData")
local function unPack(Table, destination, player)
if destination.Name == "leaderstats" then -- leaderstats handles number values
print("Found destination!")
for i, v in pairs(Table) do
local split = string.split(v, "-")
local name = split[1]
local value = split[2]
local foundValue = destination:FindFirstChild(name)
if foundValue then
print("Giving casH!")
foundValue.Value = tonumber(value)
end
end
end
if destination.Name == "OtherData" then -- other data handles string values
print("Found Other data!")
for i, v in pairs(Table) do
local split = string.split(v, "-")
local name = split[1]
local value = split[2]
local foundTool = game.ReplicatedStorage.Tools:FindFirstChild(name)
if foundTool then
print("Clone tool!")
local tool = foundTool:Clone()
tool.Parent = player.Backpack
else
print("no tool")
end
end
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 0
cash.Parent = leaderstats
local cashStorage = Instance.new("IntValue")
cashStorage.Name = "CashStorage"
cashStorage.Parent = player
local maxCash = Instance.new("IntValue")
maxCash.Name = "MaxCash"
maxCash.Value = 2500
maxCash.Parent = player
local bounty = Instance.new("IntValue")
bounty.Name = "Bounty"
bounty.Value = 0
bounty.Parent = player
local otherData = Instance.new("Folder")
otherData.Name = "OtherData"
otherData.Parent = player
local placeHolder = Instance.new("StringValue")
placeHolder.Name = "placeHolder"
placeHolder.Value = "PlaceHolder"
placeHolder.Parent = otherData
local leaderstatsData
local remainingData
local leaderstatsKey = player.UserId.."-leaderstats"
local otherDataKey = player.UserId.."-otherData"
local success, errormessage = pcall(function()
leaderstatsData = dataStore:GetAsync(leaderstatsKey)
remainingData = dataStore:GetAsync(otherDataKey)
end)
if remainingData then
print("Its k taht is the problem")
end
if success and leaderstatsData and remainingData then
unPack(leaderstatsData, leaderstats, player)
unPack(remainingData, otherData, player)
else
warn(errormessage)
end
player.CharacterAdded:Connect(function(character)
--give tools
for _, v in pairs(game.Teams:FindFirstChild(player.Team.Name):GetChildren()) do
if v:IsA("Tool") then
local newObject = v:Clone()
newObject.Parent = player.Backpack
end
end
bounty.Value = 0
local shirt = character:WaitForChild("Shirt", 30)
local pants = character:WaitForChild("Pants", 30)
if shirt and pants then
if player.Team == game.Teams.Police then
shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=1972067"
pants.PantsTemplate = "http://www.roblox.com/asset/?id=1960214"
elseif player.Team == game.Teams.Prisoners then
shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=106405750"
pants.PantsTemplate = "http://www.roblox.com/asset/?id=106405765"
else
--player team is criminal
end
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player.leaderstats:GetChildren()
local other = player.OtherData:GetChildren()
local leaderstatsKey = player.UserId.."-leaderstats"
local otherDataKey = player.UserId.."-otherData"
local leaderstatsData = {}
local remainingData = {}
for i, v in pairs(leaderstats) do
local together = v.Name .."-" .. tostring(v.Value)
table.insert(leaderstatsData, together)
end
if other[1] then
for i, v in pairs(other) do
local together = v.Name .."-" .. tostring(v.Value)
print(together)
table.insert(remainingData, together)
end
else
print("Nope")
end
local success, errormessage = pcall(function()
dataStore:SetAsync(leaderstatsKey, leaderstatsData)
dataStore:SetAsync(remainingData, otherDataKey)
end)
if success then
warn("Saved data!", player.Name)
else
warn(errormessage)
end
end)
game:BindToClose(function()
wait(7)
end)
When I print the typeof remainingData, it should be a table when I load data but its nil. I must be loading the data wrong.
Thanks for helping