Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make my leaderstats reset on player death?

Asked by 2 years ago

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
You never called "OnPlayerJoin". There is ultimately no need for it to exist as a function, either. Ziffixture 6913 — 2y

1 answer

Log in to vote
1
Answered by 2 years ago

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)
0
How would I make this only reset the "Timex" Value? Puffyjasonrocks84 39 — 2y
Ad

Answer this question