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

1 comment
You never called "OnPlayerJoin". There is ultimately no need for it to exist as a function, either.