AFK Teleport Script Not Working

Hi, so I made this afk rejoin script to prevent afk disconnects in my game. It works about half the time and I don’t know why. I have it teleporting you to another place and back every 5 minutes of being afk. When it doesn’t work, it doesn’t get to that other place. Is there anything I can do to fix this?

This is my script for teleporting to the other place:

local UIS = game:GetService("UserInputService")
local player = script.Parent.Parent.Parent.Parent
local placeid = 88367138133655

game:GetService("TeleportService"):SetTeleportGui(game:GetService("ReplicatedStorage").Teleport)

local idle = true
local timer  = 0
UIS.InputEnded:Connect(function()
	idle = true
	while wait(1) do
		timer += 1
		if not idle then
			timer = 0
			break
		end
		if timer >= 300 then
			game:GetService("TeleportService"):Teleport(placeid, player)
			break
		end
	end
end)

UIS.InputBegan:Connect(function()
	idle = false
end)

This is my script for teleporting back to the main place:

game:GetService("TeleportService"):SetTeleportGui(game:GetService("ReplicatedStorage").Teleport)
local placeid = 92666331372774

local player = script.Parent.Parent

while wait() do
	game:GetService("TeleportService"):Teleport(placeid, player)
end
3 Likes

well nevermind, ill go check the code.

try to use success, result aka pcall() function in the while loop

I recommend to break once it successes in teleporting to try and fix the issue

No, that wouldn’t work.

ReplicatedStorage is not specified anywhere.

That part of the code is completely fine

Also i recommend to retry once in a while if the afk place teleport fails using pcall()

Anyways in the code to teleport in the other place.

You didn’t really specify placeid.

Ok so ive messed with the code a bit I got it to work for me.

local UIS = game:GetService("UserInputService")
local TeleportService = game:GetService("TeleportService")
local player = game.Players.LocalPlayer
local placeId = 883671381
local idleTime = 300-- 5 minutes
local isIdle = false
local idleTimer = nil


local function teleportPlayer()
	if isIdle then
		TeleportService:Teleport(placeId, player)
	end
end


UIS.InputEnded:Connect(function()
	isIdle = true

	
	idleTimer = task.delay(idleTime, teleportPlayer)
end)


UIS.InputBegan:Connect(function()
	isIdle = false

	
	if idleTimer then
		task.cancel(idleTimer)
	end
end)

Just make sure you turn on Third Party Teleports in game settings.

i will test right now! thank you so much!

also one thing to point out is you must publish the game and play on roblox for it to work. also I putted it in starter player scripts. wich means its a local script.

yes, i know. i will let you know if it works!

also I made a small mystake just replace the game id with what you want. (I got the game id incorect when posting it).

ok, I edited the script that @rgjava made a little and it seems to work fine

here is the modified script:

local UIS = game:GetService("UserInputService")
local TeleportService = game:GetService("TeleportService")
local player = game:GetService("Players").LocalPlayer
local placeId = 88367138133655
local idleTime = 1080
local idleTimer = nil

TeleportService:SetTeleportGui(game:GetService("ReplicatedStorage"):FindFirstChild("Teleport"))

local function teleportPlayer()
		TeleportService:Teleport(placeId, player)
end

local function handleFailedTeleport(player, teleportResult, errorMessage, placeId)
	if teleportResult == Enum.TeleportResult.Flooded or teleportResult == Enum.TeleportResult.Failure then
		task.wait(2)
		TeleportService:Teleport(placeId, player)
	else
		warn(("Invalid teleport [%s]: %s"):format(teleportResult.Name, errorMessage))
	end
end

UIS.InputEnded:Connect(function()
	if idleTimer then
		task.cancel(idleTimer)
		idleTimer = nil
	end
	idleTimer = task.delay(idleTime, teleportPlayer)
end)

UIS.InputBegan:Connect(function()
	if idleTimer then
		task.cancel(idleTimer)
		idleTimer = nil
	end
end)

TeleportService.TeleportInitFailed:Connect(handleFailedTeleport)

idleTimer = task.delay(idleTime, teleportPlayer)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.