How would I save "Deaths" value using datastore?

I am not good at accessing/using datastores but I need to be able to save how many times the player dies and store it in the datastore to be accessed by each individual player here are the scripts:

SERVER SCRIPT

local event1 = game:GetService('ReplicatedStorage'):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('GameEvents'):WaitForChild('DeathCountEvent')
local retrieveDeaths = game:GetService('ReplicatedStorage'):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild("GameEvents"):WaitForChild('RetrieveDeaths')
local dataStoreService = game:GetService('DataStoreService')
local Deaths = dataStoreService:GetDataStore("Deaths")

event1.OnServerEvent:Connect(function(player, deathValue)
	local playerId = player.UserId
	local success, error = pcall(function()
		Deaths:SetAsync(tostring(playerId), deathValue)
		print("Death count saved for player:", player.Name, "Value:", deathValue)
		retrieveDeaths:FireClient(player, deathValue)
	end)
	if success then
		print("Successfully saved death count for player:", player.Name)
	else
		warn("Error saving death count:", error)
	end
end)

LOCAL SCRIPT

local player = game.Players.LocalPlayer
local remoteFunction = game:GetService('ReplicatedStorage'):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild('GameEvents'):WaitForChild('DeathCountEvent')
local remoteEvent = game:GetService('ReplicatedStorage'):WaitForChild('Events'):WaitForChild('RemoteEvents'):WaitForChild("GameEvents"):WaitForChild('RetrieveDeaths')
local deathValue = game:GetService('ReplicatedStorage'):WaitForChild('Values'):WaitForChild('Deaths')

local function updateDeathCount(value)
	deathValue.Value = value
	script.Parent.Parent:WaitForChild('Deaths#').Value = deathValue.Value
	script.Parent.Text = "Losses: "..script.Parent.Parent:WaitForChild('Deaths#').Value
	
end

local function updateKD()
	script.Parent.Parent:WaitForChild('W/LRatio').Text = "K/D Ratio: "..math.floor(script.Parent.Parent:WaitForChild('WinsValue').Value / script.Parent.Parent:WaitForChild('Deaths#').Value)
end

script.Parent.Parent:WaitForChild('Deaths#').Changed:Connect(updateKD)
script.Parent.Parent:WaitForChild('WinsValue').Changed:Connect(updateKD)

remoteEvent.OnClientEvent:Connect(updateDeathCount)

The Code Works But The DataStore Does not save the data, Thanks.

6 Likes

just save the datastore upon leaving and place the info in the player module

if you do that you will quickly reach the datastore limit

1 Like

The three problems are 1 you’re thinking you need to update the datastore every time a players death count changes. Assuming you have a either a leaderstats or intvalue that keeps track of deaths, you only need to save it when the player leaves. Secondly, don’t start a datastore from an event on a server script. Instead just use a Players:PlayerRemoving function to update your datastore. Lastly on your client script you seem to be changing a value that indicates the death count. Remember anything that a local script updates only happens on the client to the player that it gets updated to, not to the whole server. This makes your datastore not have any data to save.

1 Like

Saving when the player leaves is a bad practice. What if the server is shut down for maintenance? I’ve lost hours of progress because other developers didn’t add an auto save.

Additionally, when the player leaves the data will not be saved if they are the last player due to server shutdown. You must use BindToClose and loop through all the remaining players, save their data, and use GetAsync to make sure it is saved. You must retry this through a loop until the request is satisfied (the value is the correct one).

1 Like

Thank You although I’ve already solved this problem by adding it to my leaderstats and saving it that way.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.