I have a script where I’m saving data to my datastore but when I go and test it out solo mode, (not in studio though) game:BindToClose doesn’t seem to function which results in data not saving. My script is below and I’ve tried to remove as much irrelevant stuff from it as I can so you can see it better.
-- GENERAL VARIABLES --
local DataStoreService = game:GetService("DataStoreService")
local friendSystem = DataStoreService:GetDataStore("FriendSystem")
local friendRequests = DataStoreService:GetDataStore("FriendRequests")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Remotes = game:GetService("ReplicatedStorage"):WaitForChild("Remotes")
local data
-- SAVING DATA --
local function saveData(player)
local friendsTable = {}
Remotes.GetFriends:FireClient(player)
Remotes.GetFriends.OnServerEvent:Connect(function(player, friend)
print(friend.." - OnJoinScript Line 50")
table.insert(friendsTable, friend)
print(friend.." - OnJoinScript Line 52")
end)
wait(1)
print(friendsTable.." - OnJoinScript Line 55")
local success, errorMessage = pcall(function()
friendSystem:SetAsync("player_"..player.UserId, friendsTable)
end)
if not success then
print(errorMessage)
end
end
game.Players.PlayerRemoving:Connect(function(player)
if #Players:GetPlayers() <= 1 then return end
saveData(player)
wait(5)
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
for _, player in pairs(Players:GetPlayers()) do
saveData(player)
wait(5)
end
end)
I’ve noticed it’s got nothing to do with the saveData function because I’ve used Trello and tried printing to the console but nothing appears.
I don’t understand the friend fetching at all. Why would you ask the client for friends? Why do you need a connection? Btw. you don’t disconnect the connection so it will cause memory leaks.
You shouldn’t ask the client for such data but if you necessarily wanted to do it, you should use a RemoteFunction which is able to return the value after firing (server → client → server) so you don’t have to make any new connections. And this may be the case because I don’t see anything else is wrong with the script.
The proper way of testing BindToClose is to request closing all the servers, is this how you did it?
Thanks for replying, I’ll try to answer all your questions to the best of my abilities.
I’ll remove the waits after I get it working, I’m just keeping the waits so I can see if it works.
Sorry I didn’t explain that bit, in my game, I have a custom friend system (don’t ask me to remove it, I have my own reasons for having this) which is stored on the client, so when the player leaves, I send a request to get the players friends and wait for it to get sent back. Thanks for the suggestion about replacing them with remote functions, I haven’t used them that often which is why I haven’t included them with my current script, I’ll try to implement them later. But this isn’t the problem, because I’ve tested using multiple methods and it’s the BindToClose function that’s not working properly not the saveData function.
I’m not quite sure what you mean by this, do you mean going to the website and clicking shut down all servers? Because I’m trying to get this to work when the player is leaving the game by themselves too.
BindToClose calls on both, yes. But when you use the Shutdown button on the server, you can [if you have F9 menu open] still see things happening for a very brief moment before you are kicked. So from a testing standpoint, that’s what you’d wanna do.
As @SeargentAUS alluded to, BindToClose has a maximum time to run, so your waits actually could be the thing breaking it apart, as it’s over running the time limit.
@SeargentAUS and @GeorgeOfAIITrades 30 seconds is more than enough time for it to save and wait 5 seconds, my guess is that saving should only take around 1 second, so having the server open for about 6 seconds is all I need right now.
Correct it should only take a small moment. However not everything works perfectly. Small issues can lead to small waits, and that’ll lead to time stacking. Adding an extra 5 seconds to wait on top of can be enough to push it over.
@SeargentAUS But I’m the only player joining the game right now so it should be fine. @GeorgeOfAIITrades I’ve tried joining then leaving multiple times though and it still doesn’t work. @AlienX1258 On it’s documentation page it says SetAsync() is fine for BindToClose.
What I’ll do is I’ll try removing the waits and get back to you.
Edits: Unfortunately removing the waits didn’t work.
I also tried this but it just froze so I didn’t see anything happen.
I actually found out that they both run which means something in each of their code is stopping it from working. I tested this by bringing my Trello logs to the very start of each bit of code.
Edit: I’ve figured out that this bit of my code → if #Players:GetPlayers() <= 1 then return end in PlayerRemoving stops it from running, I’m not quite sure what it does though. And in this bit of code → for _, player in pairs(Players:GetPlayers()) do in BindToClose stops it from running.
local isClosing = false
game.Players.PlayerRemoving:Connect(function(player)
if #Players:GetPlayers() < 1 or isClosing then return end
saveData(player)
wait(5)
end)
game:BindToClose(function()
if RunService:IsStudio() then
return
end
isClosing = true