Stop giving currency when player has stopped touching part

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.

Now that does make me able to sprint but it still dosen’t stop giving me clicks when i step off the afk part.

I’ve tested it, it should work…

Still dosen’t work can you come in my game to test it out please.

I kept testing and yeah, seems like it doesn’t always work.

Do you have any solutions to that problem?

Yeah, just going to redo the code using the root part instead of making it complicated.