In the game I'm working on there's a rebirth feature where after the player rebirth, certain stats and all the tools will be removed (tools are saved in StarterGear
and I use StarterGear:ClearAllChildren()
to remove all the tools the player own).
After I rebirth, tools are gone from my StarterGear
, but after I rejoined the game, the tools I have before I rebirth came back (when it shouldn't) so I thought maybe it's a problem with my data store. Please help.
serv = game:GetService("DataStoreService") local Stats = serv:GetDataStore("StatStore") local Tools = serv:GetDataStore("ToolStore") LoadData = function(plr,level,exp) -- Stats pcall(function() local GetData = Stats:GetAsync(plr.UserId) if GetData then level.Value = GetData[1] exp.Value = GetData[2] else level.Value = tonumber(nil) exp.Value = tonumber(nil) end -- Tools local ActualTools = {} local ToolStorage = game.ReplicatedStorage.Items if (Tools:GetAsync(plr.UserId)) then for _,v in pairs(Tools:GetAsync(plr.UserId)) do print(v) if ToolStorage:FindFirstChild(v) then table.insert(ActualTools, v) end end for _,v in pairs(ActualTools) do ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("StarterGear") ToolStorage:FindFirstChild(v):Clone().Parent = plr:WaitForChild("Backpack") end end local DataLoadBoolean = Instance.new("BoolValue") DataLoadBoolean.Name = "Loaded" DataLoadBoolean.Parent = plr end) end SaveData = function(plr) -- Stats pcall(function() local lstats = plr:FindFirstChild("leaderstats") if (lstats~=nil) then local level = lstats:FindFirstChild("lol") local exp = lstats:FindFirstChild("Rebirth") if (level~=nil) and (exp~=nil) then Stats:SetAsync(plr.UserId,{level.Value,exp.Value}) end end -- Tools local ActualTools = {} local SG = plr:WaitForChild("StarterGear") for _,v in pairs(SG:GetChildren()) do table.insert(ActualTools, v.Name) end if ActualTools[1] then Tools:SetAsync(plr.UserId,ActualTools) print("data saved") end end) end game.Players.PlayerAdded:Connect(function(plr) local CanAutoSave = true local lstats = Instance.new("Folder",plr) lstats.Name = "leaderstats" local level = Instance.new("DoubleConstrainedValue",lstats) level.Name = "Weight" level.MaxValue = math.huge local exp = Instance.new("DoubleConstrainedValue",lstats) exp.Name = "Rebirth" exp.MaxValue = math.huge wait() pcall(function() LoadData(plr,level,exp) end) plr.Chatted:Connect(function(str) if str == ";save" then if CanAutoSave == true then CanAutoSave = false SaveData(plr) wait(3*60) CanAutoSave = true end end end) end) game.Players.PlayerRemoving:Connect(function(plr) if (not plr) then return end pcall(function() SaveData(plr) end) end) local ServerTimeout = 3 game:BindToClose(function() for _,v in pairs(game.Players:GetPlayers()) do if (v~=nil) then SaveData(v) end end wait(ServerTimeout) end)