local DataStoreService = game:GetService("DataStoreService")
local revolverAmmoDataStore = DataStoreService:GetDataStore("RevolverAmmoDataStore")
game.Players.PlayerAdded:Connect(function(player)
-- Create the Leaderstats folder
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
-- Create the RevolverAmmo (will be saved)
local revolverAmmo = Instance.new("IntValue")
revolverAmmo.Name = "RevolverAmmo"
revolverAmmo.Parent = leaderstats
-- Attempt to load saved data for the player
local success, data = pcall(function()
return revolverAmmoDataStore:GetAsync(tostring(player.UserId)) -- Using GetAsync correctly
end)
if success and data then
revolverAmmo.Value = data -- Assign the loaded value from the DataStore
else
revolverAmmo.Value = 0 -- If no saved data, start with 0
end
end)
game.Players.PlayerRemoving:Connect(function(player)
-- Check if RevolverAmmo is in the leaderstats folder
local revolverAmmo = player.leaderstats and player.leaderstats:FindFirstChild("RevolverAmmo")
if revolverAmmo then
-- Save the RevolverAmmo data when the player leaves
local success, errorMessage = pcall(function()
revolverAmmoDataStore:SetAsync(tostring(player.UserId), revolverAmmo.Value) -- Saving the RevolverAmmo value to the DataStore
end)
if not success then
warn("Error saving data: " .. errorMessage)
end
else
warn("RevolverAmmo not found for player " .. player.Name)
end
end)
Why does this script only save the RevolverAmmo when I win ammo and not when I lose as well?
** You are now Level 2! **