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

How would I get a script to only function if a player stood on a thing for exactly 10 seconds?

Like after 10 seconds have passed when standing on a thing something changes, but if the person gets off of it within 10 seconds the script doesn't work. Can someone please help? Thank you.

1 answer

What you can do is what I like to call "ticking". This probably has an official name but that is what I call it.

The concept is (in this case) you have a table with players as keys and a number (by default 0) as the value.

When that player touches the part, you increment that table by 1. Then, after 10 seconds, you check if the "tick" is the same as what it was when you added.

local ticks = {}

local function GetCharacter(p)
	--how i get character
	local char = p
	
	while not char:FindFirstChild("Humanoid") do
		if char:IsA("DataModel") then return end
		
		char = char.Parent
	end
end

script.Parent.Touched:connect(function(p)
	local char = GetCharacter(p)
	
	if ticks[char.Name] == nil then
		ticks[char.Name] = 0
	end
	
	ticks[char.Name] = ticks[char.Name] + 1
	
	local thisTick = ticks[char.Name]
	
	wait(10)
	
	if ticks[char.Name] == thisTick then
		--your code
	end
end)

I'll let you figure out how to make it do the same with TouchEnded to see if the player is still on it or not.

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