Stop giving currency when player has stopped touching part

I have tested it and now it only gives just 100 clicks when touching on it thats it it dosen’t give me any more clicks

Oh yeah, let me fix that for you sorry I just caught it too.

Here you go, let me know if you have any problems with this.

local part = script.Parent

local amountAdding = 100
local cooldown = 1

local function onPartTouched(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		local clicks = player.Data.PlayerData.Currency
		if not player:FindFirstChild("afkCheck") then
			local afkCheck = Instance.new("BoolValue")
			afkCheck.Name = "AFK"
			afkCheck.Parent = player
		end
		if player.AFK.Value == false then
			player.AFK.Value = true	
			repeat
				task.wait(cooldown)
				clicks.Value += amountAdding
			until otherPart.TouchEnded
			player.AFK.Value = false
		end
	end
end

part.Touched:Connect(onPartTouched)

Looks like I missed another small thing that was creating a bunch of boolValues, this should work now.

local part = script.Parent

local amountAdding = 100
local cooldown = 1

local function onPartTouched(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		local clicks = player.Currency
		if not player:FindFirstChild("AFK") then
			local afkCheck = Instance.new("BoolValue")
			afkCheck.Name = "AFK"
			afkCheck.Parent = player
		end
		if player.AFK.Value == false then
			player.AFK.Value = true	
			repeat
				task.wait(cooldown)
				clicks.Value += amountAdding
			until otherPart.TouchEnded
			player.AFK.Value = false
		end
	end
end

part.Touched:Connect(onPartTouched)

I am getting the clicks but it feels a bit slower the 1 second any solutions?

And now it doesn’t give me any clicks

You are right, let me find a solution.

give this a try

local part = script.Parent -- The part this script is attached to
local touchHasEnded

-- Function to handle the touch event
local function onPartTouched(otherPart)
	touchHasEnded = false
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		-- Check if the player has a 'Wins' leaderstats value

		local Clicks = player.Data.PlayerData.Currency
		if Clicks then
			-- Increase the player's clicks by 100 every second

			-- Check if there's already a timer running for this player
			if not player:FindFirstChild("ClicksTimer") then
				local timer = Instance.new("BoolValue")
				timer.Name = "ClicksTimer"
				timer.Parent = player

				-- Give the first win immediately, then every second				
				repeat
					Clicks.Value = Clicks.Value + 100
					task.wait(1)
				until touchHasEnded
			
			end
		end
	end
end

part.TouchEnded:Connect(onPartTouched)

part.Touched:Connect(function()
	touchHasEnded = true
end)

The script only gives me 200 clicks and thats it.

@notsqvid and @urgentnotice any solutions?

Working on it, would u be fine using a script inside the character instead or does it have to be inside of the part. A script inside the character would be more tidy if you plan to place multiple afk parts.

I don think touched is not reliable for your situation since it only runs while the player is moving. You should probably use something like Zone 3

I can have the script located inside of the charater.

Touched does work for this. Something like that isnt really required

Hes using this script for an afk area so players will probably wont be moving on the part. I dont think its possible with touched but good luck

Any solutions guys to the problem?

I got the solution. This should work.

This should be put inside of a player added connection or its own script, but the less connections the better.

game.Players.PlayerAdded:Connect(function(player)
	local afk = Instance.new("BoolValue")
	afk.Name = "AFK"
	afk.Parent = player

	local clicks = player.Data.PlayerData.Currency
	local gain = 100
	local cooldown = 1
	local db = false

	afk:GetPropertyChangedSignal("Value"):Connect(function()
		if afk.Value and db == false then
			db = true
			repeat
				task.wait(cooldown)
				clicks.Value += gain
			until afk.Value == false
			db = false
		end
	end)
end)

This belongs inside of the AFK part you are using, let me know if you face any issues.

local part = script.Parent

local amountAdding = 100
local cooldown = 1

local function onPartTouched(partThatTouched)
	local player = game.Players:GetPlayerFromCharacter(partThatTouched.Parent)
	local touchEnded
	if player then
		if player.AFK.Value == false then
			player.AFK.Value = true
			touchEnded = part.TouchEnded:Connect(function(partThatEnded)
				if partThatTouched == partThatEnded then
					player.AFK.Value = false
					touchEnded:Disconnect()
				end
			end)
		end
	end
end

part.Touched:Connect(onPartTouched)

Wait is the first script inside of “Players” or inside a player that is located in “Players”

@notsqvid Also both of the scripts completley broke my game I cannot sprint and the pets do not follow me anymore.

Sorry for not specifying, first script belongs in ServerScriptService. Second belongs inside of the AFK part.