How would I add a timer to this?

Ok so I made a basic game teleport script that makes it so when a player touches a part they get teleported to a different game but I wanted to know if its possibe to make it so the player needs to be touching the part for like 15 seconds and then it teleports them.

the basic code:

local TeleportService = game:GetService("TeleportService")
local Place = --"my games id"

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		TeleportService:Teleport(Place, player)
	end
end)

another question (you don’t need to answer this):

also is it possible to stop a player from joining a game manually and only allow them to join if they get tp’ed to a game

1 Like
local TeleportService = game:GetService("TeleportService")
local Place = --"my games id"

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then 
        task.wait(15)
		TeleportService:Teleport(Place, player)
	end
end)

Alternatively you could make a for loop counting from 15 to 0 and then tp him.

that just makes it delay for 15 seconds and all you need to do is touch it once :confused:

Setup a region part on top of the teleporting part with enough width, height and depth, and insert this code in that region part’s script. I haven’t tested it (since I’m busy making a game) so you’ll have to try it yourself to see if it works.

local Players = game:GetService("Players")
local TeleportService = game:GetService("TeleportService")
local Place = 0 --Put the ID of the place here--
local DelayTable = {}

script.Parent.Touched:Connect(function() end)
while wait(.05) do
	local IsTouching = {}
	for i,_ in pairs(DelayTable) do IsTouching[i] = false end -- Forgot to set the iterator as a pairs function, since pairs() is only usable for keyed indexes, which this table is.
	
	for _,v in ipairs(script.Parent:GetTouchingParts()) do
		local plr = Players:GetPlayerFromCharacter(v.Parent)
		
		if plr then
			IsTouching[plr.Name] = true
			if not DelayTable[plr.Name] then
				DelayTable[plr.Name] = 0
			end
			
			DelayTable[plr.Name] = DelayTable[plr.Name] + .05
			if DelayTable[plr.Name] >= 15 then
				DelayTable[plr.Name] = nil
				TeleportService:Teleport(Place, plr)
			end
		end
	end
	
	for i,v in pairs(IsTouching) do
		if not v then
			DelayTable[i] = nil
		end
	end
end
1 Like

How would I do that? would I use Region3 or something else?

It’s just a BasePart. Insert a part with CanCollide set to false, Anchored set to true, and Transparency set to 1. Then just scale that part to a size you want and that is what the players have to be within to teleport AFTER inserting the script. After 15 seconds of being in a teleporter, they will teleport. If they exit the part’s region (aka stop touching it), then they will have their timer removed from the DelayTable.

1 Like

Also, I had to fix a slight insight with a dictionary table being iterated with a ipairs() function, so just update the script with the new code so it will work.

1 Like