How to get a player's team from Touched event?

I’m writing a script where when a player touches part1, everyone in the players’ team (including the player) teleports to a location.

Here is my script so far:

local CurrentLocation = script.Parent
local Destination = script.Parent.Parent.Part2
-- (Optional), You can create local variable for each part.

CurrentLocation.Touched:Connect(function(Touch)
	-- Touched function.

	if Touch.Parent:FindFirstChild("HumanoidRootPart") then
		-- Checks if the touched part parent contains 'HumanoidRootPart'
		-- and if it does then this line of code will run.
		Touch.Parent:FindFirstChild("HumanoidRootPart").CFrame = Destination.CFrame * CFrame.new(math.random(-Destination.Size.X/2, Destination.Size.X/2),math.random(Destination.Size.Y/2, Destination.Size.Y/2),math.random(-Destination.Size.Z/2, Destination.Size.Z/2))
		-- This teleports the player to the destination.

	end

end)

This only teleports the player who touches it, not their whole team.

1 Like

You can use :GetPlayerFromCharacter

local CurrentLocation = script.Parent
local Destination = script.Parent.Parent.Part2
-- (Optional), You can create local variable for each part.

CurrentLocation.Touched:Connect(function(Touch)
	-- Touched function.

	if Touch.Parent:FindFirstChild("HumanoidRootPart") then
		-- Do stuff
		local player = game.Players:GetPlayerFromCharacter(Touch.Parent)
		-- Do more stuff
	end

end)

This grabs the player from, well, the character. You can define Players somewhere else in your script if you plan to use it more, but this should do :slight_smile:

Edit:
Use @StrongBigeMan9 's extra line at the end to ensure the player is still in the game/not needing to check for the humanoidrootpart

2 Likes

You can call :GetPlayerFromCharacter() on the Character model so that you can access the Player that touched the part. This will allow you to check their Team or TeamColor properties:

Read @Jackscarlett’s solution for something that pertains better to the direct question from the OP – I didn’t read the post well-enough and based it more off of the title, so I was only referencing one of the players like in the codeblock from the original post :slight_smile:

Old
local Players = game:GetService("Players")

local CurrentLocation = script.Parent
local Destination = script.Parent.Parent.Part2

CurrentLocation.Touched:Connect(function(Touch)

    local player = Players:GetPlayerFromCharacter(Touch.Parent)

	if player then -- This will allow you to check immediately if a part of a player's Character touched the "CurrentLocation" without needing to look for a specific part of the Character
		-- You can now reference player.Team or player.TeamColor
	end
end)
2 Likes

You can also use the Teams.TeamObject:GetPlayers() function as well :thinking:

local CurrentLocation = script.Parent
local Destination = script.Parent.Parent.Part2
local Debounce = false
-- (Optional), You can create local variable for each part.

CurrentLocation.Touched:Connect(function(Touch)
	-- Touched function.

	if Touch.Parent:FindFirstChild("HumanoidRootPart") and Debounce == false then
		-- Do stuff
        Debounce = true
		local player = game.Players:GetPlayerFromCharacter(Touch.Parent)
        if player and player.Team and not player.Neutral then

            local CurrentTeam = player.Team:GetPlayers() --Just change this to what your team is
            for _, Player in ipairs(CurrentTeam) do
                local Character = Player.Character
                if Character then
                    Character.HumanoidRootPart.Position = Destination.Position + Vector3.new(math.random(-2, 2,), 0, math.random(-2, 2)) --This is just to make sure players don't get stuck
                end
            end
        end
    wait(10)
    Debounce = false
    end

end)

I’m not sure if this works the same as game.Players:GetPlayers() but I’m guessing it does, so you can use that

Edit 1: Idk if putting the first if statement in the debounce would work, someone do please clarify me on that

Edit 2: Changed some stuff thanks to Stronge

Unrelated Below

Also:

This triggers the heck out of me :neutral_face:

3 Likes

Edit: I didn’t read the OP well-enough! @Jackscarlett is correct :slight_smile:

While this would still work for determining the team of a Player, that would be more ideal if the rest of the players of a Team were needed in order to accomplish this. Since the OP is intending to move only one Character, this will unnecessarily loop through the list of players in each Team every time the Touched event is activated (which is a lot, especially without a debounce).

And yes, I can confirm that Team:GetPlayers works the same as Players:GetPlayers in terms of retrieving a table of Players for the respective Instances.

Wait, are you sure you’re looking at the right sentence here? I think he meant this for the OP:

And not this:

Idk I’m confused, also thanks for clarifying

1 Like

Oh goodness I didn’t read the OP well enough – I was paying more attention to the title! Disregard my post then :smiley:

Ah you’re fine :sweat_smile: We all make mistakes so don’t worry about it, but yeah I’ll go ahead and add a debounce to my script as well

1 Like

One thing that I will suggest is adjusting the CurrentTeam variable to reference the player’s Team directly, since choosing a Team in the Service does not guarantee that it’ll be the Team that the player is on:

local player = game.Players:GetPlayerFromCharacter(Touch.Parent)

if player and player.Team and not player.Neutral then

    local CurrentTeam = player.Team:GetPlayers() -- Loops through the current team of the player without needing to go through the Teams service

    for _, Player in ipairs(CurrentTeam) do
        -- Continue
    end
end
2 Likes

I’m a little new to scripting. Can you tell me why debounce is necessary?

Think of a debounce as basically auto-spam

It prevents Events from firing way too quickly, & if the Player who touched the Part will teleport all players, then it’d probably happen 3-5 more times due to the Event firing cause physics exist (Fun)

The script I gave you would basically do it’s stuff, wait 10 seconds, then it’ll be ready to be touched again until another player can touch that specific part and so the loop continues

1 Like

So it’s basically a cooldown. Do I need it in the script if I only want the script to be executed once? I plan on executing CurrentLocation:Destroy() at the end of the execution, with “CurrentLocation” being the part that the player touches. My game has a round system that refreshes the map btw.

You could just remove the Debounce = false if you’d want it to only execute once, and if you want the Touched Part to be destroyed from the workspace but still visible to be accessed, you could just temporary set the CurrentLocation variable to nil, since you can obtain it back at any time inside that certain script

You’ve already been given the answer many times, please pick the post that was first and mark it as a solution. Continue your conversations in PM.