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

How would I move a player after they have been afk for 5 minutes?

I just wanna wanna make a script that does what I want above when a player is on a certain part. I understand I can use the idled event, but how would I get that to only occur when a player is on a certain part.

local players = game:GetService("Players")

local touching = false
local hit = script.Parent

players.PlayerAdded:connect(function(plr)
	
	local plrN = plr.Name
	
	script.Parent.Touched:connect(function(p)
		for i, v in pairs(players:GetPlayers()) do
			if p.Name == plrN then
				v.PlayerGui.AFKGui.Text:TweenPosition(UDim2.new(0.817, 0, 0.91, 0), "In", "Linear", 0.5)
				touching = true
				hit = p
			end
		end
	end)
	
	script.Parent.TouchEnded:connect(function()
		for i, v in pairs(players:GetPlayers()) do
			if hit.Name == plrN then
				v.PlayerGui.AFKGui.Text:TweenPosition(UDim2.new(1, 0, 0.91, 0), "In", "Linear", 0.5)
				touching = false 
			end
		end
	end)

	plr.Idled:connect(function(time)
		if hit == plr.Character and touching == true and time >= 5 then
			hit.Character.HumanoidRootPart.CFrame = hit.Character.HumanoidRootPart.CFrame * CFrame.new(0, 1, 0)
		end
	end)
	
end)

1 answer

✓ Accepted answer

Use touched and touchended to define when the player is touching, while using the idled event.

local Players = game:GetService("Players") --Predefine

touching = false --Make the variable
hit = script.Parent --Just so there aren't any glitches.

Players.PlayerAdded:connect(function(plr) --Player adde

	local plrN = plr.Name --Name our new plr

	script.Parent.Touched:connect(function(p) --Hit brick
		if p.Name == plrN then --If hit is plr?
			touching = true --Is touching
			hit = p --If you need to access the player?
		end
	end)

	script.Parent.TouchEnded:connect(function()
		if hit.Name == plrN then
			touching = false  --No longer touching
		end
	end)


	plr.Idled:connect(function(time) --Player idle
		if hit == plr.Character and touching == true then --Player Touching part? 
			--Code

		end
	end)
end)

Sources:

Idle Touched TouchEnded PlayerAdded

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