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

How do you make a teleporter work ONLY when there is at least 5 people who touch it at once?

Here is a script for a teleporter that I made:

local Teleport = workspace.TeleportPad

function Touch(hit)

if script.Parent.Locked == false and script.Parent.Parent:FindFirstChild("TeleportPad").Locked == false then script.Parent.Locked = time script.Parent.Parent:FindFirstChild("TeleportPad").Locked = true

local pos = script.Parent.Parent:FindFirstChild("TeleportPad")

hit.Parent:moveTo(workspace.TeleportPoint.Position) wait(1) script.Parent.Locked = false script.Parent.Parent:FindFirstChild("TeleportPad").Locked = false end end

script.Parent.Touched:Connect(Touch)

I want the teleporter to only work when at least 5 people (including both players and NPCs) step on it. What do I need to add to the script in order for that to happen?

2 comments

0

Please F o r m a t the script

0

yep

3 answers

you can make a table when a person touches it like this

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then -- if there is a humanoid, its a player/character
		local plrs = {}
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		table.insert(plrs,player)
		if #plrs >= 5 then
			for _,v in pairs(plrs) do
				game:GetService("TeleportService"):Teleport(yourplaceidhere,v)
			end
		end
	end
end)

`

2 comments

0

That would reset the table every time someone touches it. Make the table outside the function

0

And this wouldn't work. NPCs have Humanoids but they are not players. So this would error when trying to get the player instance from the character that touched

Hello there!

So to do this script, this is how I would do it.


local part= script.Parent
local folderOfPlayers= part.Folder
local amountofplayers= 0
local teleportPlace= 4918710826 --put your place id here
 local teleportService= game:GetService("TeleportService")
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local player= game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local playerName= hit.Parent.Name

local findValue= folderOfPlayers:FindFirstChild(playerName)
	if not findValue then
	local value= Instance.new("StringValue")
	value.Name= playerName
	value.Parent=folderOfPlayers
	amountofplayers= amountofplayers + 1
		if amountofplayers== 5 then
			for i,v in pairs(game.Players:GetChildren()) do
			local findPlayer= v.Name
				if folderOfPlayers:FindFirstChild(findPlayer) then

				teleportService:Teleport(teleportPlace, player)
			end
		end
	end
end
end
end
end)

This is quite a bit of code, so please, if this worked for you, tell me. If not, show me what error occurs.

1 comment

0

Also another note here, I may not be the most efficient at code out there, but I find tables harder to work with because Roblox Lua doesn't have many options with it compared to other languages like Python.pro

Use a table and count how many players are touching the teleporter, once the minimum is reached, teleport them

Use this:

local Teleport = workspace.TeleportPad

local teleportService = game:GetService("TeleportService")
local playerAmount = {}
local placeId = 1234567890
local minPlayers = 5

function Touch(hit)
	if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
	        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	        if #playerAmount >= minPlayers then
			for i, v in pairs(playerAmount) do
	                	teleportService:Teleport(placeId, v)

				table.remove(playerAmount, i)
			end
		end
	end
end)

script.Parent.Touched:Connect(Touch)

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