Data resetting when player dies?

when i die in my game, for some odd reason, my data resets, i dont know how.

script:

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Info")


local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Gold.Value; -- First value from the table
		player.leaderstats.WalkSpeed.Value; -- Second value from the table
		player.PlayerGui.ScreenGui.shop.Speed.max;
		player.PlayerGui.ScreenGui.shop.Speed.speedprice
	}

	local success, err = pcall(function()
		Data:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data hasn't been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function()		
		-- Currency
		
		local Leaderstats = Instance.new("Folder", p)
		Leaderstats.Name = "leaderstats"

		local gold = Instance.new("IntValue", Leaderstats)
		local walkspeed = Instance.new("IntValue", Leaderstats)
		gold.Name = "Gold"
		walkspeed.Name = "WalkSpeed"

		-- DataBase
		
		local data
		local success, err = pcall(function()
			data = Data:GetAsync(p.UserId)
		end)
		
		if success then
			walkspeed.Value = data[2]
			gold.Value = data[1]
		else
			print("Couldn't load!")
		end
		
		--p.CharacterAdded:Connect(function()
		--	p.Character.Humanoid.Died:Connect(function()
		--		p.Team = game.Teams.spectator
		--	end)
		--end)
		
		while wait() do
			p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
		end
		gold:GetPropertyChangedSignal("Value"):Connect(function()
			saveData(p)
		end)
		walkspeed:GetPropertyChangedSignal("Value"):Connect(function()
			saveData(p)
		end)
	end)
end)


game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved")
	else
		print("Data has not been saved!")
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved")
		else
			print("Data has not been saved!")
		end
	end
end)
2 Likes

This is because you are making new leaderstats each time your character respawns. This leads to you having multiple leaderstats in your player.

I mean you have a CharacterAdded event right here, obviously it will rerun the code when you die

1 Like

Try this:

local DataStoreService = game:GetService(“DataStoreService”)
local Data = DataStoreService:GetDataStore(“Info”)
local function saveData(player) – The functions that saves data
local tableToSave = {
player.leaderstats.Gold.Value, – First value from the table
player.leaderstats.WalkSpeed.Value, – Second value from the table
player.PlayerGui.ScreenGui.shop.Speed.max,
player.PlayerGui.ScreenGui.shop.Speed.speedprice
}

local success, err = pcall(function()
Data:SetAsync(player.UserId, tableToSave) – Save the data with the player UserId, and the table we wanna save
end)

if success then – If the data has been saved
print(“Data has been saved!”)
else – Else if the save failed
print(“Data hasn’t been saved!”)
warn(err)
end
end

local loadData = function(player)
local data = Data:GetAsync(player.UserId)
player.leaderstats.Gold.Value = data[1]
player.leaderstats.WalkSpeed.Value = data[2]
player.PlayerGui.ScreenGui.shop.Speed.max = data[3]
player.PlayerGui.ScreenGui.shop.Speed.speedprice = data[4]
end

game.Players.PlayerAdded:Connect(function(p)
local Leaderstats = Instance.new(“Folder”, p)
Leaderstats.Name = “leaderstats”
local gold = Instance.new(“IntValue”, Leaderstats)
local walkspeed = Instance.new(“IntValue”, Leaderstats)
gold.Name = “Gold”
walkspeed.Name = “WalkSpeed”
local success, err = pcall(function()
loadData(p)
end)
gold:GetPropertyChangedSignal(“Value”):Connect(function()
saveData(p)
end)
walkspeed:GetPropertyChangedSignal(“Value”):Connect(function()
saveData(p)
end)

p.CharacterAdded:Connect(function()
while wait() do
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
end
end)
end)

game.Players.PlayerRemoving:Connect(function(player) – When a player leaves
local success, err = pcall(function()
saveData(player) – Save the data
end)

if success then
print(“Data has been saved”)
else
print(“Data has not been saved!”)
end
end)

game:BindToClose(function() – When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do – Loop through all the players
local success, err = pcall(function()
saveData(player) – Save the data
end)
if success then
print(“Data has been saved”)
else
print(“Data has not been saved!”)
end
end
end)

this code works when i die, but when i rejoin it resets.

Try this:

local DataStoreService = game:GetService(“DataStoreService”)
local Data = DataStoreService:GetDataStore(“Info”)
local function saveData(player) – The functions that saves data
local tableToSave = {
player.leaderstats.Gold.Value,
player.leaderstats.WalkSpeed.Value,
player.PlayerGui.ScreenGui.shop.Speed.max,
player.PlayerGui.ScreenGui.shop.Speed.speedprice
}

local success, err = pcall(function()
	Data:SetAsync(player.UserId, tableToSave)
end)

end

local loadData = function(player)
local data = Data:GetAsync(player.UserId)
player.leaderstats.Gold.Value = data[1]
player.leaderstats.WalkSpeed.Value = data[2]
player.PlayerGui.ScreenGui.shop.Speed.max = data[3]
player.PlayerGui.ScreenGui.shop.Speed.speedprice = data[4]
end

game.Players.PlayerAdded:Connect(function(p)
local Leaderstats = Instance.new(“Folder”, p)
Leaderstats.Name = “leaderstats”
local gold = Instance.new(“IntValue”, Leaderstats)
local walkspeed = Instance.new(“IntValue”, Leaderstats)
gold.Name = “Gold”
walkspeed.Name = “WalkSpeed”
local success, err = pcall(function()
loadData()
end)
gold:GetPropertyChangedSignal(“Value”):Connect(function()
saveData()
end)
walkspeed:GetPropertyChangedSignal(“Value”):Connect(function()
saveData()
end)

p.CharacterAdded:Connect(function()
	while wait() do
		p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
	end
end)

end)

game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)

end)

game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
end
end)

let me just try the code quickly

i get the error attempt to index nil with leaderstats on line 5


local function saveData(player)
	local tableToSave = {
		player.leaderstats.Gold.Value; --here
		player.leaderstats.WalkSpeed.Value;
		player.PlayerGui.ScreenGui.shop.Speed.max;
		player.PlayerGui.ScreenGui.shop.Speed.speedprice
	}