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

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

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

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)

1 comment

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?

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