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
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.
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.
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)