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.
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
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
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
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)
You can also use the Teams.TeamObject:GetPlayers() function as well
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: I didn’t read the OP well-enough! @Jackscarlett is correct
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.
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
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
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