On line 124, apparently the insert will insert nothing even though the values exist, and the loop below says that the inserted value DOES exist. However, in the print statement on line 129, it says it doesn’t exist. Through testing, I can confirm that it somehow doesn’t exist.
With this datastore, I am trying to save someone’s owned sword and its equipped/unequipped value. Also, it saves two currencies: Tix and Skulls.
I am rather new to datastores so the solution could be quite simple and I just didn’t see it.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("sad")
local swordsave = {}
local function saveData(player)
local tableToSave = {
player.leaderstats.Skulls.Value;
player.leaderstats.Tix.Value;
swordsave[player.UserId]
}
if swordsave[player.UserId] ~= nil then
for i,v in pairs(swordsave[player.UserId]) do
print(player.Name, v)
end
end
local success, err = pcall(function()
print("saved")
dataStore:SetAsync(player.UserId, tableToSave)
end)
end
game.Players.PlayerAdded:Connect(function(player)
table.insert(swordsave, player.UserId)
swordsave[player.UserId] = {}
print(typeof(swordsave[player.UserId]))
local playertools = Instance.new("Folder", game.ServerStorage.Tool)
playertools.Name = player.Name
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Skulls = Instance.new("IntValue")
Skulls.Name = "Skulls"
Skulls.Parent = leaderstats
local Tix = Instance.new("IntValue")
Tix.Name = "Tix"
Tix.Parent = leaderstats
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
Skulls.Value = data[1]
Tix.Value = data[2]
if data[3] ~= nil then
swordsave = data[3]
end
if swordsave[player.UserId] ~= nil then
for i,v in pairs(swordsave[player.UserId]) do
print(player.Name, v)
end
end
end
print(typeof(swordsave[player.UserId]))
local playertools = game.ServerStorage.Tool[player.Name]
if swordsave[player.UserId] ~= nil then
for i,v in pairs(swordsave[player.UserId]) do
if typeof(v) == "string" then
local str = string.split(v, " ")
local sword = str[1]
local file = str[2]
game.ReplicatedStorage:WaitForChild("WeaponStorage"):WaitForChild(sword):Clone().Parent = game.ServerStorage:WaitForChild(player.Name):WaitForChild(file)
game.ReplicatedStorage.DataStoreUsage.DTS:FireClient(player, swordsave, str)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
saveData(player)
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
end
end)
local db = false
game.ReplicatedStorage.DataStoreUsage.val.OnServerEvent:Connect(function(player, val, boolen)
local uoe
local filex = swordsave
if filex[tostring(val) .. " Equipped"] ~= nil then
print("Unequip not worko???")
uoe = " Equipped"
elseif filex[tostring(val) .. " Unequipped"] ~= nil then
print("Equip not worko???")
uoe = " Unequipped"
end
if filex[tostring(val) .. " Unequipped"] == nil and filex[tostring(val) .. " Equipped"] == nil then
print("Added new")
uoe = " Equipped"
end
if filex ~= nil and val ~= nil and uoe ~= nil and boolen ~= nil then
for i,v in pairs(filex) do
local dont = false
if filex[tostring(val) .. " Unequipped"] ~= nil then
print("deleted")
local found = table.find(filex, val .. " Unequipped")
filex[found] = nil
dont = true
end
if filex[tostring(val) .. " Equipped"] ~= nil then
if dont == false then
print("deleted")
local found = table.find(filex, val .. " Equipped")
filex[found] = nil
end
end
dont = false
end
table.insert(filex, tostring(val) .. uoe)
print("insertted ", filex, tostring(val) .. uoe)
for i,v in pairs(filex[player.UserId]) do
print(v)
end
print(filex[tostring(val) .. uoe])
end
end)