Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a onTouch event check if the player is on a team?

Ik this script isn't checking the player's team I've only defined blue at the top but IDK how to make it so it's checking if the player is on a certain team while touching, also any help on making it only fire once while they first touch it would be great! Thanks in advance I appreciate it, you guys are really helping me learn.

local blue = game.Teams.Blue:GetPlayers()

function yes() print("Doing Something") end

game.Workspace.Zone.Touched:Connect(yes)

Pastebin: https://pastebin.com/gUC4H4wa

1 answer

✓ Accepted answer

You can have it only fire once by using :Disconnect on the Touched event.

local Players = game:GetService('Players")
local Teams = game:GetService("Teams")

local Part = workspace:WaitForChild("Zone")

function CheckForTeam(PlayerTouched, Team)
	for _,Player in pairs(Teams[Team]:GetPlayers()) do
		if (Player == PlayerTouched) then
			return true
		end
	end
	return false
end

Part.Touched:Connect(function(Touched)
	local Player = Players:GetPlayerFromCharacter(Touched.Parent)
	if (CheckForTeam(Player, "Blue")) then
		print(Touched.Parent.Name.." is allowed to touch this part")
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.