Saving data to private server

I want to make my game only work in private servers, so when the player joins it should retrieve the data from that private server, but how would I do that?

Normaly I would save my data to the player, but now let’s say a friend of the player joins it won’t be able to retrive the data.

Check if it is a private server, and load/save data from that point.
https://create.roblox.com/docs/reference/engine/classes/DataModel#PrivateServerId

Your game would use the data in a datastore like normal, but if PrivateServerId == "" then simply do not run the necessary data loading or saving.

Yes I understand, I’m not sure if this will work because I can’t test it in studio:

local dataStore = game:GetService("DataStoreService")
local serverData = dataStore:GetDataStore("ServerData")

if game.PrivateServerId ~= "" then return end
if game.PrivateServerOwnerId ~= 0 then return end

local money = game.ReplicatedStorage.Money

local getData = serverData:GetAsync(game.PrivateServerOwnerId)

money.Value = getData

game:BindToClose(function()
	serverData:SetAsync(game.PrivateServerOwnerId,money.Value)
end)

This likely would not work. the return method is used to exit a function—it does not end the thread or stop it from running further.

In order to do this, try structuring it like this:

local dataStore = game:GetService("DataStoreService")
local serverData = dataStore:GetDataStore("ServerData")

if game.PrivateServerId == "" then -- if the game is not a private server, PrivateServerId will be ""
	return -- end the saving stuff here
else
	local money = game.ReplicatedStorage.Money
	local getData = serverData:GetAsync(game.PrivateServerOwnerId)

	money.Value = getData

	game:BindToClose(function()
		serverData:SetAsync(game.PrivateServerOwnerId,money.Value)
	end)
end

but you would need to replace this if statement with a function which makes sense for you

1 Like