Team member limiter not working

so i want to limit the number of players in the persimmon colored team but with my current script, all players just avoid that team and will always be selected into the storm blue colored team.
script:

local Jteam = teams.Juggernaut
local val = 0

function onTouch(hit)
	if val == 0 then
		hit.Parent.Humanoid.MaxHealth = 500
		hit.Parent.Humanoid.Health = 500

		game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).TeamColor = BrickColor.new("Persimmon")
		val = 1
	end

	if val == 1 then
		hit.Parent.Humanoid.MaxHealth = 100
		hit.Parent.Humanoid.Health = 100

		game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).TeamColor = BrickColor.new("Storm blue")
	end

	if game.ReplicatedStorage.Status.Value == 'Round over!' then
		val = 0
	end
end

script.Parent.Touched:Connect(onTouch)```

I believe the variable val is being updated even though the object which touched the brick is not a child of a player’s character. Therefore, val is being increased in number even if it’s just the floor below that touched the brick.

Here, try this:

function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") == nil then
		return
	end
	if game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
		return
	end

	--Insert the rest of your function here

end

In this code sample, I added a portion of the script that’ll check to see if the part that touched the object is a part of a humanoid’s character. If not, then it will return nil and therefore the function ends using return. After it confirms that the part is a child of a character, it ensures that the character is a character of a player, rather than an NPC or a bot, using game.Players:GetPlayerFromCharacter(hit.Parent) == nil. If the character does not belong to a player, it will return nil, and therefore will end the function using return.

These lines of code will ensure that the code will not run unless the part touching belongs to a player’s character. The first player that touches this part should be teamed on the Persimmon team.

I hope this helps, and sorry if that was a lot to read. I can help further with this if required!

1 Like

thank you for the help! i will try this when i get home

wait nevermind this doesnt work since each player joins in at the same exact time. how should i approach this?

alright, so i fixed my issue by improving on my debounce and adding an if statement to check if the player is in the persimmon team before switching a player to the storm blue team
this is the working code:

local db = false

function onTouch(hit)
	if hit.Parent:FindFirstChild("Humanoid") == nil then
		return
	end
	if game.Players:GetPlayerFromCharacter(hit.Parent) == nil then
		return
	end
	
	if db == false then
		hit.Parent.Humanoid.MaxHealth = 500
		hit.Parent.Humanoid.Health = 500

		game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).TeamColor = BrickColor.new("Persimmon")
		db = true
	end

	if db == true then
		if game:GetService("Players"):getPlayerFromCharacter(hit.Parent).TeamColor == BrickColor.new("Persimmon") then
			return
		end
		hit.Parent.Humanoid.MaxHealth = 100
		hit.Parent.Humanoid.Health = 100

		game:GetService("Players"):GetPlayerFromCharacter(hit.Parent).TeamColor = BrickColor.new("Storm blue")
	end
	
	if game.ReplicatedStorage.Status.Value == 'Round over!' then
		db = false
	end
end

script.Parent.Touched:Connect(onTouch)```
2 Likes