Issue with team switching

Hello Developers,

I recently came across an issue with my team script detecting if there’s a certain amount of people on a team. For some reason, when players spam clicks the “Host” button, it allows them all to go on the Host team even if I’m detecting that there are 0 players on the team.

Does anyone have any clue on how I can solve this? I’ve tried basically everything I could come up with and there was really no solid solution.

I’m posting my code below.

local ModuleSettings = {
	["Group Configuration"] = {
		["GroupId"] = PermissionsModule["GroupId"],
		["Developer Rank"] = PermissionsModule["Developer"],
		["Server Host Rank"] = PermissionsModule["Server Host"],
	},
	["Gamepass Configuration"] = {
		["Unlimited Team Package"] = GamepassModule["Unlimited Teams"],
		["Server Host"] = 1689373
	},
	["Cooldown Table"] = {},
	["Previous Host"] = "",
	["Host"] = ""
}

-- Code
SwitchTeamRemote.OnServerEvent:Connect(function(Sender, SelectedTeam)
	if not ModuleSettings["Cooldown Table"][Sender] then
		ModuleSettings["Cooldown Table"][Sender] = 0
	end
	if tick() - ModuleSettings["Cooldown Table"][Sender] >= 2.5 then
		ModuleSettings["Cooldown Table"][Sender] = tick()
		local RepStore = DatastoreModule("rep", Sender)

		if SelectedTeam == "Host" then
			if Sender.Team == TeamService.Host then
				return
			end

			if Sender.Name == ModuleSettings["Previous Host"] then
				NotificationRemote:FireClient(Sender, {
					["Message"] = "You were the previous host!",
					["Status"] = "Error"
				})
				return
			end

			ModuleSettings["Previous Host"] = Sender.Name

			if #TeamService.Host:GetPlayers() == 0 then
				if MarketPlaceService:UserOwnsGamePassAsync(Sender.UserId, ModuleSettings["Gamepass Configuration"]["Unlimited Team Package"]) or MarketPlaceService:UserOwnsGamePassAsync(Sender.UserId, 692897) or Sender.Name == "OS_92" then
					Sender.Team = TeamService.Host
					Sender:LoadCharacter()
				else
					if RepStore:Get(0) >= 25 then
						Sender.Team = TeamService.Host
						Sender:LoadCharacter()
						RepStore:Increment(-25)
					end
				end
			else
				NotificationRemote:FireClient(Sender, {
					["Message"] = "The Host team is full!",
					["Status"] = "Error"
				})
			end
      end)

Use time() instead of tick()
You are setting “Previous Host” before actually making the player host.
Can you try printing #TeamService.Host:GetPlayers() before the if check?
Your gamepass check (and presumably your repstore check) yields your code.

Yep, just realized I was setting the previous host to that. I actually just did that not long before posting this forum, so it didn’t go live on all servers. I’ve also tried the printing method, it always says 0 but when the remote is fired at the same time by two people both people get the host position.

Since your code is being yielded by the gamepass check, and possibly the datastore check as well, the team count check can pass for one player, halt before setting team as it asks roblox whether the player owns the gamepass or not, then set them to the team, meaning there’s actually a delay before the player gets put in the host team, thus allowing multiple players to enter said team.

I’ll test this out and let you know what happens

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