Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make my leaderstats reset on player death?

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

1

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

1 answer

✓ Accepted answer

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)

1 comment

0

How would I make this only reset the "Timex" Value?

This discussion is preserved as it was. New answers, comments, and votes are closed.