In short
game:BindToClose() runs a function when a server shuts down, usually to save everyone's data, so only server code may call it. A LocalScript that calls it, or a ModuleScript in ReplicatedStorage that a LocalScript requires, gets this error. Call BindToClose from a Script in ServerScriptService. The variant BindToClose failed because the model is already being closed means the call came too late, while the server was already closing.
What the message means
ReplicatedStorage.DataManager:31: BindToClose can only be called on the server.
Other forms of the same message:
BindToClose failed because the model is already being closed.
The path in the message shows which script ran the line. A module path such as ReplicatedStorage.DataManager means a shared module, which runs on whichever side required it.
Players leaving do not need BindToClose: Players.PlayerRemoving handles that. BindToClose covers the moment the whole server closes, when PlayerRemoving might not get time to finish.
Causes and fixes
Two things cause this error in practice. Each one below has the smallest code that triggers it, then the fix.
A shared module calls BindToClose
A data module in ReplicatedStorage sets up saving when it loads. When a LocalScript requires it for its helper functions, the BindToClose line runs on the client.
-- ReplicatedStorage.DataManager
local DataManager = {}
function DataManager.saveAll()
print("saving")
end
game:BindToClose(function()
DataManager.saveAll()
end)
return DataManager
Move the data module to ServerScriptService, where clients cannot require it. Keep only code the client needs in ReplicatedStorage.
-- ServerScriptService.DataManager
local DataManager = {}
function DataManager.saveAll()
print("saving")
end
game:BindToClose(function()
DataManager.saveAll()
end)
return DataManager
A LocalScript tries to save on close
The client cannot save to data stores, and has no shutdown hook. Code that saves must run on the server.
game:BindToClose(function()
print("goodbye")
end)
Save in a server Script, both when a player leaves and when the server closes.
-- a Script in ServerScriptService
local Players = game:GetService("Players")
local function save(player: Player)
print("saving", player.Name)
end
Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
for _, player in Players:GetPlayers() do
task.spawn(save, player)
end
task.wait(3)
end)
The server waits up to 30 seconds for BindToClose functions to finish. Saving players in parallel with task.spawn keeps you well inside that.
Fix it in your place
Paste the error, RoCode fixes it in your place
Copy the line from Output and paste it here. RoCode opens the script it names in the place you have open in Studio, works out which of the causes above it is, and edits the code there instead of handing you a snippet to paste.
RoCode is an AI agent for Roblox Studio: it reads the scripts in the place you have open, finds the line and edits it there, and every change is an undo point. It runs on Windows or macOS with a desktop browser. Free to start, with free credits every day.
How to find the line that causes it
- Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
- Find the first red line. It starts with the script's place in the Explorer and the line number, such as
ServerScriptService.Shop:12:. - Click the red line. Studio opens that script with the cursor on the failing line.
- Read the lines between
Stack BeginandStack Endunder it: they list the calls that led there, newest first, so you can see which function called the broken one. - Still not sure which value is wrong? Add
print(typeof(x), x)just above the line, or set a breakpoint on it and read the values in the Watch window.
Questions
Do I need BindToClose if I save on PlayerRemoving?
Yes. When the server shuts down, for example after an update, PlayerRemoving may not finish for everyone. BindToClose gives you time to save the players who are still there.
Does BindToClose run in Studio?
Yes, when you stop a play test. Saving in Studio needs Enable Studio Access to API Services on.
What does model is already being closed mean?
BindToClose was called while the server was already shutting down, too late to register. Call it once, at the top level of a server Script, when the server starts.
Can RoCode fix it?
Yes. Paste the Output line into RoCode. It moves the shutdown code to a server Script in your open place and checks the saving code.
Sources and further reading
- Roblox Creator Docs: DataModel:BindToClose
- Roblox Creator Docs: data stores
- Roblox Creator Docs: Players.PlayerRemoving
How the code was checked: every block marked Fixed passes Luau's strict type checker against Roblox's API definitions (luau-lsp, 12 November 2026), and every block marked Breaks compiles and fails only when it runs. The message wording was checked against the strings in Roblox Studio itself (macOS build installed on 12 November 2026) on 12 November 2026. A type check catches misspelt APIs and wrong types, not game logic, so playtest in Studio before you publish.