I have a leaderstats for time called "Timex" that I want to reset when the player dies here is my leaderstats script:
local DataStoreService = game:GetService("DataStoreService") local myDataStore = DataStoreService:GetDataStore("myDataStore") game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Timex = Instance.new("IntValue") Timex.Name = "Time" Timex.Parent = leaderstats local Wins = Instance.new("IntValue") Wins.Name = "Wins" Wins.Parent = leaderstats local function onPlayerJoin(player) player.CharacterAdded:Connect(function(char) char.Humanoid.Died:Connect(function() Timex.Value = 0 end) end) end local data local success, errormessage = pcall(function() data = myDataStore:GetAsync(player.UserId.."-Wins",player.leaderstats.Wins.Value) end) if success then Wins.Value = data else print"There was an error whilst getting your data! :(" end end) game.Players.PlayerRemoving:Connect(function(player) local success, errormessage = pcall(function() myDataStore:SetAsync(player.UserId.."-Wins", player.leaderstats.Wins.Value) end) if success then print("Player Data successfully saved! :)") else print("There was an error when saving your data :(") warn(errormessage) end end)
Thanks
local function onPlayerAdded(plr) local con1 plr.CharacterAdded:Connect(function(char) repeat wait() until char.Parent and char:FindFirstChild("Humanoid") con1 = char.Humanoid.Died:Connect(function() for i, v in pairs(plr.leaderstats:GetChildren()) do if tostring(v.ClassName):find("Value") then v.Value = 0 end end end) repeat wait() until not char.Parent con1:Disconnect() end) end game.Players.PlayerAdded:Connect(onPlayerAdded)