How would i use this in my script? Can you show me?
You would just put that code at the bottom. When I talked about your PlayerRemoving
code, you could convert it to this:
local function save(player:Player)
--saving code goes here
end
players.PlayerRemoving:Connect(save)
and that would allow you to do this in the BindToClose
:
--remember to get the services for this at the top of the script.
--This code can go at the bottom of the script.
game:BindToClose(function()
if runService:IsStudio() or #players:GetPlayers() <= 1 then
task.wait(3)
return nil
end
for _, player in next, players:GetPlayers(), nil do
save(player) --a lot easier than re-typing all the code.
end
task.wait(3)
end)
Iām really stupid but what would i put in the save function?
I havenāt tested that yet, sorry for not responding.
it doesnāt do it at another place
All your saving code. Hereās the script, all updated:
local ServerScriptService = game:GetService("ServerScriptService")
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local MarketplaceService = game:GetService("MarketplaceService")
local DataStoreService = game:GetService("DataStoreService")
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local runService = game:GetService("RunService")
--// Variables \\--
local startTime = os.clock()
local loadTime = ReplicatedStorage:WaitForChild("loadTime")
local serverModulesFolder = ServerScriptService:WaitForChild("ServerModules")
local modules = ReplicatedStorage:WaitForChild("Modules")
local ConfigurationModule = modules:FindFirstChild("Configuration")
local Configuration = require(ConfigurationModule)
local statsVersion = ReplicatedStorage:WaitForChild("gameVersion").Value
local skipId = Configuration.ProductIds.skipId
local dataStore = DataStoreService:GetDataStore("PlrDataStore")
local function load(player:Player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local stage = Instance.new("IntValue")
stage.Parent = leaderstats
stage.Name = "Stage"
stage.Value = 1
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Value = 0
coins.Parent = leaderstats
local success, data = pcall(dataStore.GetAsync, dataStore, player.UserId)
if not success then
warn("Error retrieving player data for "..player.Name..": "..data)
player:Kick("Failed to load data. Please rejoin!")
return nil
end
if not data then
data = {
["Stage"] = 0,
["Coins"] = 0
}
end
stage.Value = data.Stage
coins.Value = data.Coins
end
local function save(player:Player)
local data = {
["Stage"] = player.leaderstats.Stage.Value,
["Coins"] = player.leaderstats.Coins.Value
}
local success
local result
local attempt = 0
repeat
success, result = pcall(dataStore.UpdateAsync, dataStore, player.UserId, function(old)
return data
end)
attempt += 1
until
success or attempt == 3
if not success then
warn(result)
end
end
local function onGameClose()
if runService:IsStudio() or #players:GetPlayers() <= 1 then
task.wait(3)
return nil
end
for _, player in next, players:GetPlayers(), nil do
save(player)
end
task.wait(3)
end
players.PlayerAdded:Connect(load)
players.PlayerRemoving:Connect(save)
game:BindToClose(onGameClose)
local endTime = os.clock()
local elapsedTime = (endTime - startTime) * 75000
loadTime.Value = elapsedTime
(script is untested, let me know if you get errors)
I removed your developer product purchase processor because you should definitely make it more robust and do it in a seperate script. Hereās the official structure I would recommend off of the documentation:
ProcessReceipt
(this includes purchase history saving but that is not necessary.)
That confirms what I previously said: thereās nothing wrong with your code. You probably have one of the test scripts enabled accidentally