Custom World ID Not Saving (Profile Service)

So below I’ve got this code I made with profile service… First, I would like to know how I could possibly remove this remote handler from this script and put it in another script in serverscriptservice so I can get the player’s world data and data and stuff without being in the main template script… Secondly, I’d like to fix this issue I’m having. When a player presses a button on the world GUI, it’ll create a unique ID for the world they’ve selected. It’ll then teleport the player to the specific world using the ID. If the ID is already set, it’ll teleport them without making a new ID. When I press the button, it teleports me after 3 seconds. When I press join on the world again after already being teleported, it instantly teleports me meaning the ID did correctly save and it’s being detected; but when I get teleported to the world again, it kicks me for “Roblox has shut the server down for maintenance.” This could be because I’m trying to teleport to the same ID again. But the problem is, whenever I leave after being teleported to the world, the ID doesn’t save and it creates a new one again…

This post is referenced from my question below:

Here’s the code:


local ProfileTemplate = {
	
	WorldInformation = {
		World1 = {
			Name = "WORLD 1",
			ID = "None",
			FirstLogin = "None",
			LastSave = "None",
			Inventory = {},
			Tools = {},
			Structures = {},
		},
		
		World2 = {
			Name = "WORLD 2",
			ID = "None",
			FirstLogin = "None",
			LastSave = "None",
			Inventory = {},
			Tools = {},
			Structures = {},
		},
		
		World3 = {
			Name = "WORLD 3",
			ID = "None",
			FirstLogin = "None",
			LastSave = "None",
			Inventory = {},
			Tools = {},
			Structures = {},
		},
	},
	
	
	Settings = {
		
		IncomingWorldInvites = true, -- CAN RECIEVE WORLD JOIN REQUESTS
		PlayersCanEditWorld = false, -- PLAYERS IN WORLD CAN PLACE AND DESTROY STRUCTURES
		PlayersCanInteractWithWorld = false, -- PLAYERS IN WORLD CAN INTERACT WITH NPCS AND PLAYER STRUCTURES
		LowEndDeviceMode = false, -- RUNNING ON A LOW END DEVICE
		AllowPlayerJoins = false, -- PLAYERS CAN JOIN YOUR WORLD AFTER GETTING AN INVITE OR WORLD BEING FEATURED
		ReduceParticleEffects = false, -- LOWER PARTICLE EMMITTER COUNT
		JukeboxMusic = true, -- PLAYER CAN HEAR MUSIC FROM JUKEBOX
		RecordingStreamingMode = false -- HIDES SENSITIVE INFORMATION DURING GAMEPLAY (WORLD ID, ETC)
		
	},
	
	Achievements = {
		FirstStarted = false, -- PLAYER PLAYED FOR FIRST TIME
		FirstAxe = false, -- PLAYER OBTAINED FIRST AXE
		FirstPickaxe = false, -- PLAYER OBTAINED FIRST PICKAXE
		FirstChoppedTree = false, -- PLAYER CHOPPED FIRST TREE
		FirstMinedMineral = false, -- PLAYER MINED FIRST ROCK / MINERAL
		Reached1000Coins = false, -- PLAYER REACHED 1K COINS
		Reached5000Coins = false, -- PLAYER REACHED 5K COINS
		Reached15000Coins = false, -- PLAYER REACHED 15K COINS
		FirstStructure = false, -- PLAYER PLACED FIRST STRUCTURE
		FirstBlueprintFilled = false, -- PLAYER FILLED FIRST BLUEPRINT
		Reached1Hour = false, -- PLAYER PLAYED FOR 1 HOUR TOTAL
		Reached24Hours = false, -- PLAYER PLAYED FOR 24 HOURS TOTAL
		Played50Times = false, -- PLAYER LOGGED ON 50 TIMES
		Jukebox = false, -- PLAYER OBTAINED JUKEBOX FOR THE FIRST TIME
		FirstEnchant = false, -- PLAYER ENCHANTED THEIR FIRST TOOL
		Lucky = false, -- PLAYER GOT VERY LUCKY AT RANDOM CHANCES
		Reached5Achievements = false, -- PLAYER GOT 5 ACHIEVEMENTS
		Reached10Achievements = false, -- PLAYER GOT 10 ACHIEVEMENTS
		PlayedLumberium = false, -- PLAYER PLAYED LUMBERIUM BEFORE
		PurchasedInLumberium = false, -- PLAYER PURCHASED SOMETHING WITH ROBUX IN LUMBERIUM
	},
	
	LogInTimes = 0,
}

----- Loaded Modules -----

local ProfileService = require(game.ServerScriptService.Data.ProfileService.ProfileService)

----- Private Variables -----

local Players = game:GetService("Players")

local ProfileStore = ProfileService.GetProfileStore(
	"WorldData",
	ProfileTemplate
)

local Profiles = {} -- [player] = profile

----- Private Functions -----

local function GiveCash(profile, amount)
	-- If "Cash" was not defined in the ProfileTemplate at game launch,
	--   you will have to perform the following:
	if profile.Data.Cash == nil then
		profile.Data.Cash = 0
	end
	-- Increment the "Cash" value:
	profile.Data.Cash = profile.Data.Cash + amount
end

local function DoSomethingWithALoadedProfile(player, profile)
	profile.Data.LogInTimes = profile.Data.LogInTimes + 1
	print(player.Name .. " Logged In " .. tostring(profile.Data.LogInTimes)
		.. " time" .. ((profile.Data.LogInTimes > 1) and "s" or ""))
end

local function PlayerAdded(player)
	 profile = ProfileStore:LoadProfileAsync("Player_" .. player.UserId)
	if profile ~= nil then
		profile:AddUserId(player.UserId) -- GDPR compliance
		profile:Reconcile() -- Fill in missing variables from ProfileTemplate (optional)
		profile:ListenToRelease(function()
			Profiles[player] = nil
			-- The profile could've been loaded on another Roblox server:
			player:Kick("Rejoin; Data Save Attempted On Two Different Occasions")
		end)
		
		if player:IsDescendantOf(Players) == true then
			Profiles[player] = profile
			-- A profile has been successfully loaded:
			print(player.Name .. " DATA LOAD COMPLETE!")
			DoSomethingWithALoadedProfile(player, profile)
		else
			-- Player left before the profile loaded:
			profile:Release()
		end
		local DataLoaded = Instance.new("StringValue",player)
		DataLoaded.Name = "DataLoaded"
	else
		-- The profile couldn't be loaded possibly due to other
		--   Roblox servers trying to load this profile at the same time:
		player:Kick("Rejoin; Data Save Thrown Before Server Save") 
	end
	
	print(game.JobId)
end

----- Initialize -----

-- In case Players have joined the server earlier than this script ran:
for _, player in ipairs(Players:GetPlayers()) do
	task.spawn(PlayerAdded, player)
end

----- Connections -----

Players.PlayerAdded:Connect(PlayerAdded)

Players.PlayerRemoving:Connect(function(player)
	local profile = Profiles[player]
	if profile ~= nil then
		profile:Release()
	end
end)


game:GetService("ReplicatedStorage").Game.DatastoreRemotes.Requests.RequestEnterWorld.OnServerEvent:Connect(function(Player, WorldNumber)

	local ProfileServiceModule = require(game.ServerScriptService.Data.ProfileService.ProfileService)
	
	if profile then

		if WorldNumber == 1 then

			if profile.Data.WorldInformation.World1.ID == "None" then

				local TeleportService = game:GetService("TeleportService")

				local NewID = TeleportService:ReserveServer(game.PlaceId) -- This will return a code which can be used to teleport the player or players

				profile.Data.WorldInformation.World1.ID = NewID
				
				print(NewID)

				TeleportService:TeleportToPrivateServer(game.PlaceId,NewID,{Player})

			else

				local TeleportService = game:GetService("TeleportService")

				local ID = profile.Data.WorldInformation.World1.ID
				
				print("ID ISNT NONE IT IS: " .. ID)
 				TeleportService:TeleportToPrivateServer(game.PlaceId,ID,{Player})

			end

		elseif WorldNumber == 2 then

			print("testing for now don't use")

		elseif WorldNumber == 3 then
			
			print("testing for now don't use")

		end

	else
		warn("Can't Enter World - No Data Found For: " .. Player.Name)
		return
	end


end)
1 Like