Teleporting all players to specific player

Hello can someone help with my code, Im trying to teleport all players to a specific player my code is:

script.Parent.Touched:Once(function(hit)
    local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if player then
        for i, v in pairs(game.Players:GetPlayers()) do
            v.Character:PivotTo(player.Character:WaitForChild("HumanoidRootPart").CFrame)
        end
    end
end)

so this script must teleport all players to the player who touched the part but it does teleport the player who touched the part to him self how can I fix I only want to teleport all players to the player who touched the part

1 Like
for i, v in pairs(game.Players:GetPlayers()) do
if v.Name == player.Name then
continue
end
           v.Character:PivotTo(player.Character:WaitForChild("HumanoidRootPart").CFrame)
 end

see if this works for you, might have to move the end.

Try this:

script.Parent.Touched:Connect(function(hit)
	local SpecificPlayer = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

	if player then

		for _, player in pairs(game.Players:GetPlayers()) do
			if player.Name ~= SpecificPlayer.Name then
				player.Character:PivotTo(SpecificPlayer.Character:WaitForChild("HumanoidRootPart").CFrame)
			end
			end
		end
	end)
1 Like

it does not work but I donā€™t get any error

have you tried mine? I did update it

i tried the script its says missing end this my full 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
				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
end)

Move the end below ā€œcontinueā€[][][][]

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)