Roblox Studio error

BindToClose can only be called on the server

What BindToClose can only be called on the server means in Roblox Studio, the two things that cause it with the code that triggers each, and the fix for each one.

Updated 12 November 20263 min readErrorLuauWritten by

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.

On this page
  1. What it means
  2. Causes and fixes
  3. Fix it with RoCode
  4. Find the line
  5. Questions
  6. Sources

What the message means

Output (example)
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.

Breaks ModuleScript in ReplicatedStorage named DataManager
-- 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.

Fixed ModuleScript in ServerScriptService named DataManager
-- 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.

Breaks LocalScript in StarterPlayer.StarterPlayerScripts named Goodbye
game:BindToClose(function()
    print("goodbye")
end)

Save in a server Script, both when a player leaves and when the server closes.

Fixed Script in ServerScriptService named SaveData
-- 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.

For example: ReplicatedStorage.DataManager:31: BindToClose can only be called on the server.

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

  1. Open Output (Window menu, or the Script tab), clear it with Ctrl+K (⌘K on Mac), and press Play.
  2. Find the first red line. It starts with the script's place in the Explorer and the line number, such as ServerScriptService.Shop:12:.
  3. Click the red line. Studio opens that script with the cursor on the failing line.
  4. Read the lines between Stack Begin and Stack End under it: they list the calls that led there, newest first, so you can see which function called the broken one.
  5. 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

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.

More Roblox errors

All Roblox Studio errors

Stop hunting errors line by line.

RoCode reads the scripts in your open place, fixes the error where it happens and tells you what it changed. Free every day, no card required.