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.