Training Hub scripting help

Hello! I am stuck on creating a script that lets a user who is in the group and their rank is above 235 open a server. When the user opens a server it will let players in that game to join the server but it would be locked for now. It may be complicated to explain but should I use a private server script for this or is there another way I can script this?

Here is a screenshot of what I mean!
image

so first of all to check if the player is in the group and that rank or above to this

if  Player:GetRankInGroup(groupid) >= 235 then
print("Player has required rank!")
end

For creating the “Private Server” you should use TeleportService:ReserveServer on a server script by using a remote event (make sure to check if they are the correct rank on the server!

Then you can use DataStoreService to store the Reserved Server Codes and read them.

For example you would do something like this on the server

local TeleportService = game:GetService("TeleportService")
local PlaceId = 0 --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)

RemoteEventToMakeAServer.OnServerEvent:Connect(function(Plr,data) 
	if  Player:GetRankInGroup(groupid) >= 235 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)

RemoteEventToJoinAServer.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
	
	RemoteEventToSendServersToClient:FireAllClients(Servers)
end

On the client you would put something like this


ButtonToMakeServer.MouseButton1Click:Connect(function()
  RemoteEventToMakeAServer:FireServer({Name = textboxNameofServerInput .Text})
end)

RemoteEventToSendServersToClient.OnClientEvent:Connect(function(Servers) 
    -- make sure to clear the server list frame here
    for i,Data in pairs(Servers) do 

     local Name = Data.ServerData.Name
     local PlayerList = Data.ServerData.Plrs
     local  ReserveServerCode = Data.ReservedServerCode

     -- make the ui stuff

      button.MouseButton1Click:Connect(function() 
        RemoteEventToJoinAServer:FireServer(ReserveServerCode)
     end
     end

end)

Now on the training servers have something like this under a server script

local Http = game:GetService("HttpService")
local ReserveServerCode = ""
local GotReserveServerCode = false
local UpdateInterval = 15
local DataStoreService = game:GetService("DataStoreService")
local options = Instance.new("DataStoreOptions")
options.AllScopes = true
local ReservedServers = DataStoreService:GetDataStore("Servers","",options)
local ReservedServer

game.Players.PlayerAdded:Connect(function(player) 
	if not GotReserveServerCode  then 
		if player:GetJoinData().TeleportData then
			GotReserveServerCode   = true
			ReserveServerCode = player:GetJoinData().TeleportData
                      ReservedServer =  Http:JSONDecode(ReservedServers:GetAsync(ReserveServerCode))

		end
	end
end)

-- All we need to do next is update the player list

while wait(UpdateInterval ) do
       local Players  = game:GetService("Players")
        local UserIdTable = {}
      for i,v in pairs(Player:GetPlayers) do
       table.insert(UserIdTable,v.UserId)
      end

      ReservedServers:SetAsync(ReserveServerCode,Http:JSONEncode({Name  = ReservedServer.Name, Plrs = UserIdTable, Creator = ReservedServer.Creator})
end

-- Now when the game closes we remove the Server
   

game:BindToClose(function()
	ReservedServers:RemoveAsync(ReserveServerCode)
end)

This is basically how you would do it

man this took so long to write, hope I helped.

2 Likes

For the UI bit I have created it but when I click on the button it does nothing

you got to define the button, dont do do button.

this was an example

Alrighty I will do it tomorrow as it is getting late I will hopefully get back to you tomorrow Ty for the help for now!

I know it has been soo long but I don’t think this is what I meant since it wasn’t working. I mean once a user clicks a button, it makes them join the server they made. In the server they have made, there would be 2 buttons for the server host. 1 which would hide the server for ranks below 59 and show for ranks >= 59. And another button that shows the server for everyone. Once the server has shutdown, the server would remove from the list. I have tried to script this but I wasn’t that successful, it only cloned the server for the player who clicked the button and didn’t tp them to the server.