DataStore doesn't Save Data and Survivals get added to players even after they die

Title explains issue. The code is below. All help is appreciated!

local Players = game:GetService("Players")
local playerTable = {}
local function onPlayerDied(player)
	-- Your logic for handling player death goes here
	print(player.Name .. " has died")
	for i, p in ipairs(playerTable) do
		if p.player == player then
			p.isAlive = false
		end
	end
end

local function startNewRound()
	-- Clear the table at the start of a new round
	playerTable = {}
	-- Re-add all players to the table
	for _, player in ipairs(Players:GetPlayers()) do
		table.insert(playerTable, { player = player, isAlive = true })
	end
end
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local survivals = Instance.new("IntValue")
	survivals.Name = "Survivals"
	survivals.Value = 0
	survivals.Parent = leaderstats

	-- Load player's data from the data store
	local userId = player.UserId

	-- Attempt to retrieve the player's survival data from the data store
	local success, data = pcall(function()
		return playerDataStore:GetAsync(userId)
	end)

	if success then
		-- Check if the data contains survival stats
		if data.survivalStats then
			-- Update the player's survival leaderstat with the retrieved data
			player.leaderstats.Survival.Value = data.survivalStats
		else
			-- If no survival stats are found, initialize the leaderstat with a default value
			player.leaderstats.Survival.Value = 0
		end
	else
		warn("Failed to retrieve survival data for player " .. player.Name)
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerDied)


local function endRound()
	for _, p in ipairs(playerTable) do
		if p.isAlive then
			local leaderstats = p.player:FindFirstChild("leaderstats")
			if leaderstats then
				local survivals = leaderstats:FindFirstChild("Survivals")
				if survivals then
					survivals.Value = survivals.Value + 1
				end
			end
		end
	end
end

1 Like

This happens because your checking if p.isAlive exists but not if it’s false or true
(i can however be also incorrect as i don’t really do many things with tables atm)

1 Like

Instead of trying to find the issue i just decided to change it a little to make it work obviously not the best but i’m still not that good as other scripters hope this helps tho

local Players = game:GetService("Players")
local playerTable = {}
local function onPlayerDied(player)
	-- Your logic for handling player death goes here
	print(player.Name .. " has died")
	if playerTable[player.Name] then
		playerTable[player.Name]["isAlive"] = false
	end
end

local function startNewRound()
	-- Clear the table at the start of a new round
	playerTable = {}
	-- Re-add all players to the table
	print("Starting")
	for _, player in ipairs(Players:GetPlayers()) do
		playerTable[player.Name] = {
			["isAlive"] = true
		}
	end
end
local DataStoreService = game:GetService("DataStoreService")
local playerDataStore = DataStoreService:GetDataStore("PlayerData")
local function onPlayerAdded(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local survivals = Instance.new("IntValue")
	survivals.Name = "Survivals"
	survivals.Value = 0
	survivals.Parent = leaderstats

	-- Load player's data from the data store
	local userId = player.UserId

	-- Attempt to retrieve the player's survival data from the data store
	--local success, data = pcall(function()
	--	return playerDataStore:GetAsync(userId)
	--end)

	--if success then
	--	-- Check if the data contains survival stats
	--	if data.survivalStats then
	--		-- Update the player's survival leaderstat with the retrieved data
	--		player.leaderstats.Survival.Value = data.survivalStats
	--	else
	--		-- If no survival stats are found, initialize the leaderstat with a default value
	--		player.leaderstats.Survival.Value = 0
	--	end
	--else
	--	warn("Failed to retrieve survival data for player " .. player.Name)
	--end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerDied)
Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:FindFirstChild("Humanoid").Died:Connect(function()
			onPlayerDied(plr)
		end)
	end)
end)


local function endRound()
	print("Ended")
	for _,player in pairs(playerTable) do
		if player["isAlive"] == true then
			local plr = game.Players:FindFirstChild(player)
			if plr then
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats then
					local survivals = leaderstats:FindFirstChild("Survivals")
					if survivals then
						survivals.Value += 1
					end
				end
			end
		end
	end
end
task.wait(5)
startNewRound()
task.wait(10)
endRound()

btw i commented out the lines for data loading because i didn’t wanna add datastores to my testing place

1 Like

This doesn’t add survivals at all when I try using it.

Worked for me so i wouldn’t know on why it doesn’t for you

EDIT: you could probably add a failsafe incase it doesn’t add it for anyone if it doesn’t award it at all

Found the issue inside the code forgot to change a bit of code while playing around with some older code i had my bad
here’s what you should change endRound() to:

local function endRound()
	print("Ended")
	for plrname,player in pairs(playerTable) do
		if player["isAlive"] == true then
			local plr = Players:FindFirstChild(plrname)
			if plr then
				local leaderstats = plr:FindFirstChild("leaderstats")
				if leaderstats then
					local survivals = leaderstats:FindFirstChild("Survivals")
					if survivals then
						survivals.Value += 1
					end
				end
			end
		end
	end
end

this should hopefully work now