BindToClose code not working?

Hey,

This will sound most likely weird lol but why isn’t it printing when I shut down all servers? Is it because BindToClose is faster than the code or something? In my perspective, it should print. But it isn’t.

local ServerStorage = game:GetService("ServerStorage")
local Greenwich = require(ServerStorage:WaitForChild("Greenwich"))

game:BindToClose(function()
	print("[GREENWICHDB/QUEUE] The queue is now ending to ensure that no data is lost.")
	Greenwich:EndQueue()
end)

Keep in mind that Greenwich is a DataStore wrapper.

Thanks in advance!

1 Like

change line 4 to

game.BindToClose:Connect(function()
1 Like

BindToClose is a function, not an event.

3 Likes

Yeah. Thought of that too after checking official documents. Anything else what could be causing it?

1 Like

If you test the code in-studio, it is possible it may not print. If you shut down all available servers, you can’t see the content printed. Try storing something in datastores Roblox serves, then get the data on your end to check whether it works.

2 Likes

I tested it all in-game, not studio. I could try what you said.

1 Like

Add a wait(4) at the end of the function.

local ServerStorage = game:GetService("ServerStorage")
local Greenwich = require(ServerStorage:WaitForChild("Greenwich")) or require(ServerStorage.Greenwich)

if not Greenwich then
    warn("Server: Greenwich wasn't found!")
end

pcall(function()
    game:BindToClose(function()
	    Greenwich:EndQueue()
    end)
end)

print("Success.")

Pcalls are one of the best uses for these types of things! Hopefully this helps you! :slightly_smiling_face:

1 Like

What is this going to help with? I don’t think the script is erroring at all. Pcalls only stop errors from breaking the script. So if a value is nil it will stay nil.

Almost a week ago with no activity means it’s irrelevant. Please don’t furthermore respond.

While a pcall() isn’t always a solution, adding a wait also isn’t a solution. Yielding the code does absolutely nothing except yielding the code. Additionally, wait() is mostly outdated, and it is recommended to use task.wait()

A pcall() (protected call) assures the part of the code won’t error. Greenwich shouldn’t be nil, meaning it shouldn’t need to error in the first place. If Greenwich is nil, there’s the solution right there. Thanks for your time.

1 Like

Pcalls should only be used when you are using methods that don’t always work or error sometimes. However, they have never once said that their code was erroring.

I gave a suggestion to put wait(4) into the game:BindToClose. This is because roblox studio shuts down too fast. This could be why their code isn’t working. I have no clue if they have tested it out.

Respectfully, do your research before replying and/or trying to back up your information. Thank you.

(This is my last response, I won’t be responding after this.)

Please do read the original post before creating an argument. The original post states this:

pcall() is most commonly found in DataStores, meaning I did use them correctly. Adding a wait() on a DataStore isn’t a solution at all. No research is needed when the post literally states Greenwich is a DataStore wrapper.

Whilst they may not have said it was erroring, it certainly could’ve but behind the scenes. (Quite a common occurance.)

BindToClose also runs while the game is shutting down, to prevent data loss. I don’t believe a wait yielding the BindToClose will be a help, it will just slow it down.

All in all, please read the fine details before targeting someone over the right step in the right direction. :slightly_smiling_face:

2 Likes

This will also be my final message, unless the previous thread owner needs help.

Even if its a DataStore wrapper, you still need to wait. I have made a tutorial telling why! :smiley:
Also, we do not know what the mainmodule looks like. I’m pretty sure they have a pcall, because all datastore tutorials have them. (Also has it on the developer hub, too)

Thanks!

1 Like