hello, I am making a data store script but I am getting this issue
,bind to close doesn’t work
here is an example:
game:BindToClose(function()
for i,player in pairs(players:GetChildren()) do
local Data
local Get,Error = pcall(function()
Data = datastore:GetAsync(player.UserId.."-data")
end)
---------------------------
Data.pointData = player.PlayerData.points.Value
local Success ,Err = pcall(function()
datastore:SetAsync(player.UserId.."-data",Data)
end)
print("saved")
end
wait(5)
end)
local Players = game:GetService("Players")
game:BindToClose(function(player)
for _, player in ipairs(Players:GetPlayers()) do
local s, e = pcall(function()
DataStore:SetAsync(player.UserId..'-data', player.PlayerData.points.Value) --setting data
print("Saved!")
if not s then TestService:Error(e)
print("Not Saved!")
end
end
end)
end)
You’re correct, the BindToClose() callback doesn’t even have a parameter so player in the above example would point to nil.
Additionally, BindToClose() is executing for the server, not for each client individually.
local players = game:GetService("Players")
game:BindToClose(function()
for _, player in ipairs(players:GetPlayers()) do
--Save each player's data here.
end
end)