Stop giving currency when player has stopped touching part

Wait Im sorry but can you do it to this script

local part = script.Parent – The part this script is attached to

local function onPartTouched(otherPart)
	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 wins by 1 every 5 minutes
			local function giveClicks()
				Clicks.Value = Clicks.Value + 100
			end

			-- 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 5 minutes
				giveClicks()
				while timer and timer.Parent do
					task.wait(1) -- 5 minutes
					giveClicks()
				end
			end
		end
	end
end


-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)

I dont get why your adding leaderstats when the player touches the part. If you want to access the leaderstats by touching the part just send a bindable events to the leaderstats script.

wait why dont you want to add a leaderstat script

1 Like

I have added a ui which shows the clicks. Also I have changed the script to this local part = script.Parent -- The part this script is attached to

local function onPartTouched(otherPart)
	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 wins by 1 every 5 minutes
			local function giveClicks()
				Clicks.Value = Clicks.Value + 100
			end

			-- 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 5 minutes
				giveClicks()
				while timer and timer.Parent do
					task.wait(1) -- 5 minutes
					giveClicks()
				end
			end
		end
	end
end


-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)

You are making alot of unnecessary checks

just make a leaderstat script that adds this leaderstat and there is no need to make this section. It really does improve workflow and makes finding the issue easier.

Your just going to need this script to do the calculations then send the bindable event at the end of the function of the value that you want to add.

From your script I see that you want to add 100 Clicks to the Clicks leaderstat every 5 minutes. You can simplify this script to

local event = game:GetService("ServerStorage").Bindables.Event

local clicks = 0
local function onPartTouched(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)
	if player then
		while otherPart.Parent == player do
			event:Fire(100)
			task.wait(300)
		end
	end
end

The leaderstats script is going to receive this and add 100 clicks value that way

i think i messed up the touch function hold on

Grabbed this from this post

this is the script keep it mind it will only print after 300 seconds you can lower this to test for yourself

local part = script.Parent
local event = game:GetService("ServerStorage").Bindables.Event
local touchHasEnded

local clicks = 0


part.TouchEnded:Connect(function()
	touchHasEnded = false
	repeat
		event:Fire(100)
		clicks+=100
		task.wait(300)
	until touchHasEnded
	print(clicks)
end)


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

Ok I want when I touch the part it gives me 100 clicks per second and when I step off the part it stops giving me clicks.

Script:

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

-- Function to handle the touch event
local function onPartTouched(otherPart)
	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
			local function giveClicks()
				Clicks.Value = Clicks.Value + 100
			end

			-- 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
				giveClicks()
				while timer and timer.Parent do
					task.wait(1) -- 1 second
					giveClicks()
				end
			end
		end
	end
end





-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)

I will even send video
robloxapp-20240630-1128360.wmv (2.7 MB)
In the video, when I step off the AFK Grinder part it keeps giving me 100 clicks per second where I want it to stop giving me clicks. I hope you can understand this

Also don’t mind the rebirths I was trying to set my clicks to zero but realised I diddn’t have enough clicks to rebirth.

1 Like

OP, can you try using this script? Let me know if you find anything wrong

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

local amountAdding = 100
local cooldown = 1

-- Function to handle the touch event
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
			afkCheck.Value = true
		elseif player.AFK.Value == false then
			local afkCheck = player.AFK
			afkCheck.Value = true
		end

		repeat
			task.wait(cooldown)
			clicks.Value += amountAdding
		until otherPart.TouchEnded
	end
end

-- Connect the touch event to the function
part.Touched:Connect(onPartTouched)

That does work but it sometimes gives me 200 - 300 clicks and it doesn’t give the clickes every second it gives me the clicks even faster

1 Like

Made a bit of an oopsie, try now. (AGAIN!)

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
		end
	end
end

part.Touched:Connect(onPartTouched)

It now doesn’t give me the clicks at all😢 Wait ima try it again

Did you try using the one I revised just now or hmm, make sure these are the same cause i just changed it. Yes.

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