There are a few problems along with some general logic issues. You can't use DataStoreService in a LocalScript. If you aren't, then you can't get LocalPlayer in a normal script. Since you said your script isn't crashing, I'm going to assume it breaks before that while statement without a wait, else you'd be crashing. You have not set a key for the player, therefore you'd just be saving the same key over and over again to one store 'PlayerDeaths. You shouldn't update _G.died in a while statement since there's a limit. Finally, I recommend using a NumberValue placed in the player V.S. a shared variable since there are restrictions between Local and server scripts.
First, place a Script in ServerScriptService. This script will place the NumberValue in the player and load their deaths to it. We use this to keep track of the deaths and display it in the GUI.
Put a LocalScript in StarterCharacterScripts. Put a Script in the LocalScript. The LocalScript will wait for things to load and the Script will handle everything important, since we don't want exploitation of our value. When FilteringEnabled is on, the client aka you or any player in your game can change things, but it will only be visible by you.
Put your GUI in PlayerGui with a LocalScript in it. The GUI's script will detect a change in the NumberValue. Although this can be exploited, it's not going to do anything but tell you something false if they do. Nothing saves here.
This is a version where it saves every time the player dies. For a large game, this wouldn't be ideal, as there are limits.
This is the code inside the Script in ServerScriptService
local DataStoreService = game:GetService("DataStoreService")
local playerDeathsStore = DataStoreService:GetDataStore("PlayerDeaths")
game.Players.PlayerAdded:Connect(function(player)
local key = string.format("%s_%s", player.Name, player.userId)
local deaths = Instance.new("NumberValue", player)
deaths.Name = "Deaths"
pcall(function()
local data = playerDeathsStore:GetAsync(key) or 0
deaths.Value = data
end)
end)
This is the code inside the LocalScript in StarterCharacterScripts
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
local deaths = player:WaitForChild("Deaths")
local RemoteEvent = script:WaitForChild('RemoteEvent')
humanoid.Died:Connect(function()
RemoteEvent:FireServer(player)
end)
This is the code inside the Script placed in the LocalScript
local DataStoreService = game:GetService("DataStoreService")
local playerDeathsStore = DataStoreService:GetDataStore("PlayerDeaths")
local RemoteEvent = Instance.new("RemoteEvent", script.Parent)
local function save(player, new_value, key)
if pcall(function()
playerDeathsStore:UpdateAsync(key, function()
return new_value
end)
end) then
print(string.format("Saved %s deaths [%s]", player.Name, new_value))
else
print(string.format("Couldn't save %s deaths.", player.Name, new_value))
end
end
RemoteEvent.OnServerEvent:connect(function(player)
local deaths_val = player:findFirstChild("Deaths")
local key = string.format("%s_%s", player.Name, player.userId)
if deaths_val then
deaths_val.Value = deaths_val.Value + 1
save(player, deaths_val.Value, key)
end
end)