Help finding when a player isn't touching a part

Hi, Scripters!
I’m trying to make it so when a player touches a part, their leaderstats go up one second at a time and when they aren’t touching the part, it pauses. Also, when the player dies, their leaderstat should return to 0.
The issues I’ve run into include:

  • Increasing the leaderstat when the player isn’t moving but is still on the part
  • Detecting when the player is no longer touching the part
local TimePart = game.Workspace.TimePad.TimePart
local Players = game:GetService("Players")
local Touching = false

--Debounce
TimePart.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player then
		if not Touching then
			Touching = true
			Player.leaderstats.Time.Value += 1
			wait(1)
			Touching = false
		end
	end
end)




while wait() do
	for i,player in pairs(game.Players:GetChildren()) do
		local Character = player.Character or player.CharacterAdded:Wait()
		if Character:WaitForChild("Humanoid").Health == 0 then
			player.leaderstats.Time.Value = 0
		end
	end
end

Thanks to anyone that can help! I’ll be here to reply if you can respond :slight_smile:

1 Like

Just use .TouchEnded, check if its the player and then do your code.

What Im trying to do is pause the increase in the leaderstats while the block isn’t being touched. Can you explain how I would do that? Also I still am not sure how the player will keep its currency increasing while they aren’t moving because even though I have a touched event, when the player isnt moving, the number doesn’t increase.

1 Like

GetPartsInPart seems perfect for this:

--Script inside StarterCharacterScripts
local Players = game:GetService("Players")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local Player = Players:GetPlayerFromCharacter(Character)
local Time = Player:WaitForChild("leaderstats"):WaitForChild("Time")

local TimePart = workspace.TimePad.TimePart
local Touching = false

--Use Humanoid.Died instead of looping
Humanoid.Died:Connect(function()
	Time.Value = 0
end)

function isCharacter(parts)
	for _, part in pairs(parts) do 
		if part.Parent == Character then
			return true
		end
	end
	return false 
end

while task.wait(1) do 
	local parts = workspace:GetPartsInPart(TimePart)
	--if any of the parts touching are parts of player character
	if isCharacter(parts) then 
		--increase their time
		Time.Value += 1
	end
end
1 Like

Would this be in the StarterCharacterScripts?

1 Like

Works like a charm! Thanks so much for your help!

1 Like