What is wrong with my Training Hub code?

Hello CookieTech community! I would like to know why this code doesn’t work and returns this error:

Script:

local TeleportService = game:GetService("TeleportService")
local PlaceId = 18682365053 --Must be apart of the universe
local UpdateTime = 15
local DataStoreService = game:GetService("DataStoreService")
local options = Instance.new("DataStoreOptions")
options.AllScopes = true
local ReservedServers = DataStoreService:GetDataStore("Servers","",options)

game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(Plr,data) 
	if  Plr:GetRankInGroup(8702939) >= 5 then
		print("Player has required rank!")
		local ReservedServerCode = TeleportService:ReserveServer(PlaceId)



		ReservedServers:SetAsync(ReservedServerCode,{Name  = data.Name,Plrs = {},Creator = Plr.UserId})

		TeleportService:TeleportToPrivateServer(PlaceId,ReservedServerCode,{Plr},nil,ReservedServerCode)
	end
end)

game.ReplicatedStorage.JoinServer.OnServerEvent:Connect(function(Plr,ReservedServerCode) 
	ReservedServers:SetAsync(ReservedServerCode,"")

	TeleportService:TeleportToPrivateServer(PlaceId,ReservedServerCode,{Plr})
end)


while wait(UpdateTime) do
	local Servers = {}
	local listSuccess, pages = pcall(function()
		return ReservedServers:ListKeysAsync()

	end)

	if listSuccess then
		while true do
			local items = pages:GetCurrentPage()
			for _, v in ipairs(items) do
				table.insert(Servers,v.KeyName)
			end
			if pages.IsFinished then
				break
			end
			pages:AdvanceToNextPageAsync()
		end
	end

	game.ReplicatedStorage.SendToClient:FireAllClients(Servers)
end

I would like to recieve feedback on this code. Thank you!

:wave: Hey, I checked out your script and made a few changes! :smile:

I noticed a couple of things that could be improved, like removing the unnecessary DataStoreOptions and adding some error handling so it doesn’t crash if something goes wrong. I also fixed the part where it lists the servers to make sure it doesn’t get stuck in a loop.

Here’s what I came up with:

local TeleportService = game:GetService("TeleportService")
local PlaceId = 18682365053
local UpdateTime = 15
local DataStoreService = game:GetService("DataStoreService")
local ReservedServers = DataStoreService:GetDataStore("Servers")

game.ReplicatedStorage.CreateServer.OnServerEvent:Connect(function(Plr, data)
    if Plr:GetRankInGroup(8702939) >= 5 then
        print("Player has required rank!")
        local ReservedServerCode = TeleportService:ReserveServer(PlaceId)
        
        local success, err = pcall(function()
            ReservedServers:SetAsync(ReservedServerCode, {Name = data.Name, Plrs = {}, Creator = Plr.UserId})
        end)
        if not success then
            warn("Failed to save reserved server data: " .. tostring(err))
        end

        TeleportService:TeleportToPrivateServer(PlaceId, ReservedServerCode, {Plr})
    end
end)

game.ReplicatedStorage.JoinServer.OnServerEvent:Connect(function(Plr, ReservedServerCode)
    local success, err = pcall(function()
        ReservedServers:SetAsync(ReservedServerCode, nil)
    end)
    if not success then
        warn("Failed to update reserved server data: " .. tostring(err))
    end

    TeleportService:TeleportToPrivateServer(PlaceId, ReservedServerCode, {Plr})
end)

while wait(UpdateTime) do
    local Servers = {}
    local listSuccess, pages = pcall(function()
        return ReservedServers:ListKeysAsync()
    end)

    if listSuccess then
        while true do
            local items = pages:GetCurrentPage()
            for _, v in ipairs(items) do
                table.insert(Servers, v.KeyName)
            end
            if pages.IsFinished then
                break
            end
            local success, err = pcall(function()
                pages:AdvanceToNextPageAsync()
            end)
            if not success then
                warn("Failed to advance to the next page: " .. tostring(err))
                break
            end
        end
    else
        warn("Failed to list reserved servers")
    end

    game.ReplicatedStorage.SendToClient:FireAllClients(Servers)
end

Hope this helps! Let me know if anything else needs fixing or if you run into any other issues.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.