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

How to make it so that the data won't reset when a player died?

Asked by 2 years ago

This is the Script that I use can someone improve this?

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local DpointsData = DataStore:GetDataStore("DpointsData1")
local RankData = DataStore:GetDataStore("RankData1")

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)

local Player = game:GetService("Players").LocalPlayer
        local stats = Player:WaitForChild("leaderstats")

        local Dpoints = stats.Dpoints
        Dpoints.Value = DpointsData:GetAsync(Player.UserId) or 0
        DpointsData:SetAsync(Player.UserId, Dpoints.Value)

        local Rank = stats.Rank
        Rank.Value = RankData:GetAsync(Player.UserId) or "Unrank"
        RankData:SetAsync(Player.UserId, Rank.Value)
    end)

    game.Players.PlayerRemoving:Connect(function(Player)
        DpointsData:SetAsync(Player.UserId, Player.leaderstats.Dpoints.Value)
        RankData:SetAsync(Player.UserId, Player.leaderstats.Rank.Value)
    end)    

    game:BindToClose(function()
        DpointsData:SetAsync(Player.UserId, Player.leaderstats.Dpoints.Value)
        RankData:SetAsync(Player.UserId, Player.leaderstats.Rank.Value)
    end)
end)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

For starters, you should not use LocalPlayer in a server script. You should delete the Player.CharacterAdded function, it is not needed, then that should fix it.

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local DpointsData = DataStore:GetDataStore("DpointsData1")
local RankData = DataStore:GetDataStore("RankData1")

Players.PlayerAdded:Connect(function(Player)
        local stats = Player:WaitForChild("leaderstats")

        local Dpoints = stats.Dpoints
        Dpoints.Value = DpointsData:GetAsync(Player.UserId) or 0
        DpointsData:SetAsync(Player.UserId, Dpoints.Value)

        local Rank = stats.Rank
        Rank.Value = RankData:GetAsync(Player.UserId) or "Unrank"
        RankData:SetAsync(Player.UserId, Rank.Value)
    end)

    game.Players.PlayerRemoving:Connect(function(Player)
        DpointsData:SetAsync(Player.UserId, Player.leaderstats.Dpoints.Value)
        RankData:SetAsync(Player.UserId, Player.leaderstats.Rank.Value)
    end)    

    game:BindToClose(function()
        DpointsData:SetAsync(Player.UserId, Player.leaderstats.Dpoints.Value)
        RankData:SetAsync(Player.UserId, Player.leaderstats.Rank.Value)
end)
0
The script solve the problem. however, another problem came, the Player on line 24 and below can't be recognize by the script. can you help me more? AltairCelestia 47 — 2y
Ad

Answer this question