Teleporting all players to specific player

Just like this.

script.Parent.Touched:Once(function(hit)
	local SpecificPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if SpecificPlayer then
		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Name == SpecificPlayer.Name then
				continue
			end
				v.Character:PivotTo(SpecificPlayer.Character:WaitForChild("HumanoidRootPart").CFrame)
				opened.Value = false
				tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
				closeSound:Play()
		end
	end
end)

Try this out:

local PS = game:GetService("Players")

script.Parent.Touched:Once(function(hit: BasePart)
	local char = hit:FindFirstAncestorWhichIsA("Model")
	local player = char and PS:GetPlayerFromCharacter(char)
	
	if player then
		for _,targetPlayer: Player in ipairs(player:GetPlayers()) do
			if not targetPlayer.Character or targetPlayer == player then
				continue
			end
			
			local targetChar = targetPlayer.Character
			
			targetChar:PivotTo(char:GetPivot())
		end
	end
end)

you didnt have to rescript the whole thing, you only just needed an if statement… ._.

1 Like

Sometimes ‘hit.Parent’ isn’t directly a body part, there’s a chance for it to detect accessories. It’s better safe than sorry.

could just do a repeat until plr ~= nil

but i still agree with you, his detection isnt really safe.

I see the problem

Did you see the line

if player then

Change it to

if SpecificPlayer then

pretty sure all you have to do is to add an if statement

if v ~= player then
-- whatever thing you need to do
end

Sorry for the delayed response, Anyways This script works a little fine, the issue is that if there is just one player. for example, if the player is playing solo the entire function will not be activated. You need more than 1 player to make it active How can this be fixed?

here is the script

local frame = script.Parent.Parent
local closeSound = frame:WaitForChild("DoorClose")
local model = frame.Parent
local frameClose = model:WaitForChild("DoorFrameClose")
local frameOpen = model:WaitForChild("DoorFrameOpen")
local opened = model:WaitForChild("Opened")
local tweenService = game:GetService("TweenService")


script.Parent.Touched:Once(function(hit)
	local SpecificPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if SpecificPlayer then
		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Name == SpecificPlayer.Name then
				continue
			end
			v.Character:PivotTo(SpecificPlayer.Character:WaitForChild("HumanoidRootPart").CFrame)
			opened.Value = false
			tweenService:Create(frame,TweenInfo.new(.35),{CFrame = frameClose.CFrame}):Play()
			closeSound:Play()
		end
	end
end)

I recommend you use :GetTouchingParts() instead of trusting .Touched as it may cause issues.

This is what I did:

local MaxAmountOfPlayers = 1 -- Sets the Max Amount of players who can be on it. ( I set it to one because I have no friends to test it with..)

local Players = game:GetService("Players") -- Get the Players

local PlayersTouchingPart = {}; -- This is our table to see who is touching it

function UpdateTable()
	local ListOfPartsTouching = script.Parent:GetTouchingParts() -- Getting all the parts touching script.Parent
	local Added = false
	for _,player in pairs(Players:GetPlayers()) do -- Loop through players
		
		local Character = player.Character or player.CharacterAdded:Wait() -- Get Character
		
		local Hrp = Character.HumanoidRootPart -- Get HumanoidRootPart
		
		for _, part in pairs(ListOfPartsTouching) do  -- Loop through the touching parts list
			if part.Parent:FindFirstChild("HumanoidRootPart") then -- Check if the part's parent has a HumanoidRootPart.
				if part.Parent.HumanoidRootPart == Hrp then -- Compare the players and this parts HumanoidRootPart
					if table.find(PlayersTouchingPart, Hrp) then -- Check if they are already in the table
						Added = true -- Set added true
					else
						if #PlayersTouchingPart < MaxAmountOfPlayers then -- Check if the table is full
							table.insert(PlayersTouchingPart, Hrp) -- Add them to the table
							Added = true -- Set added true
						end
					end
				end
			end
		end
		if Added then -- Checking if they were added 
			Added = false 
		else
			if not Added then -- They weren't added!
				if table.find(PlayersTouchingPart, Hrp) then -- Check if they are in the table
					table.remove(PlayersTouchingPart, table.find(PlayersTouchingPart, Hrp)) -- Yeet them from the table.
				end
			end
		end
	end
end

script.Parent.Touched:Connect(function() -- You have to have this to allow the :GetTouchingParts() to work.
end)

while task.wait(1) do -- I set mine up to a loop.
	UpdateTable() -- Updates the table
	if #PlayersTouchingPart == MaxAmountOfPlayers then -- Check if the Players on the Part is equal to the max.
		-- Proceed with what you were doing
		print(PlayersTouchingPart[1].Parent) -- This is how you get the first player's HumanoidRootPart who touched it
	end
end

you have “Once” instead of Connect

Can you do this instead. Also you’re telling me how to run the function if they’re playing solo?

script.Parent.Touched:Connect(function(hit)

He said that there are multiple players needed to activate it.

Oh, so like it needs 2 people to touch the part and it works?

I was assuming he meant that which is why I wrote this whole giant code.
Which allows you to set the Max Players needed to activate.

@Koki0991 So, just give more context if you can so that I could help you better.

Here’s a script for you.

local Connection

Connection = script.Parent.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	
	if Player and Player.Character and Player.Character.HumanoidRootPart then
		for i, v in pairs(game.Players:GetPlayers()) do
			if v.Character and v ~= Player then
				local NewPivot = v.Character:GetPivot() * CFrame.new(Player.Character.HumanoidRootPart.Position)
				v.Character:PivotTo(NewPivot)
			end
		end
		
		print("Teleported players because "..Player.Name.." activated")
		Connection:Disconnect()
	end
end)

As it seems you originally wanted, judging by your use of :Once() this event will only fire one time
If you would like to change this functionality, remove the

Connection:Disconnect()

Hope this is what you’re looking for!

my game contains solo + 2 - 4 players if I set it to 4 players max can 1 player active it?

nvm that works thanks for help

You are welcome. Have an amazing day! Let us know if you need anymore help.

1 Like