Hey everyone, So my data request keep getting to the max then I cant save data anymore how would I make it save the players data when the leave the game or the servers shut down
local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LeaderStatSave")
local killsDataStore = DataStore:GetDataStore("KillsDataStore")
local gloveDataStore = DataStore:GetDataStore("GloveDataStore")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Set up the RemoteEvent for purchases
local purchaseEvent = ReplicatedStorage:FindFirstChild("PurchaseEvent")
if not purchaseEvent then
purchaseEvent = Instance.new("RemoteEvent")
purchaseEvent.Name = "PurchaseEvent"
purchaseEvent.Parent = ReplicatedStorage
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Pushes = Instance.new("NumberValue", leaderstats)
Pushes.Name = "Pushes"
Pushes.Value = ds:GetAsync(player.UserId .. "-Pushes") or 0
local Glove = Instance.new("StringValue", leaderstats)
Glove.Name = "Arm"
Glove.Value = gloveDataStore:GetAsync(player.UserId .. "-Glove") or script.StarterGlove.Value
local function saveStats()
ds:SetAsync(player.UserId .. "-Pushes", Pushes.Value)
gloveDataStore:SetAsync(player.UserId .. "-Glove", Glove.Value)
end
Pushes.Changed:Connect(saveStats)
Glove.Changed:Connect(saveStats)
end)
-- Save data when player is leaving the game
game.Players.PlayerRemoving:Connect(function(player)
local leaderstats = player:FindFirstChild("leaderstats")
if leaderstats then
ds:SetAsync(player.UserId .. "-Pushes", leaderstats.Pushes.Value)
gloveDataStore:SetAsync(player.UserId .. "-Glove", leaderstats.Arm.Value)
end
end)
-- Handle purchase requests
purchaseEvent.OnServerEvent:Connect(function(player, itemName, price)
local leaderstats = player:FindFirstChild("leaderstats")
if not leaderstats then return end
local coins = leaderstats:FindFirstChild("Pushes")
local arm = leaderstats:FindFirstChild("Arm")
if coins and arm and coins.Value >= price then
coins.Value = coins.Value - price
arm.Value = itemName
gloveDataStore:SetAsync(player.UserId .. "-Glove", arm.Value)
ds:SetAsync(player.UserId .. "-Pushes", coins.Value)
else
warn("Purchase failed for " .. player.Name .. ": not enough Pushes or missing leaderstats.")
end
end)