Stop giving currency when player has stopped touching part

Read the comments inside of that code box.

Nope still dosent want to give me any clicks.

This is getting to complicated to give instructions, I could join your game if you want but have no idea how to help from here without access otherwise.

Yea can you join my game as team create add me my username is planeboy2021

1 Like

I sent a request, just let me know when I can join.

It says you need to be part of my group to be able to edit in my game

Are you global6s? or notsqvid?

1 Like

I gotto go cya I might be back later today

local part = script.Parent

local addedClicks = 100

local connection = nil

local function CreateTimer(player)
	local timer = Instance.new("BoolValue")
	timer.Name = "ClicksTimer"
	timer.Parent = player

	return timer
end

local function OnTouched(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)

	if player then
		local Clicks = player.Data.PlayerData.Currency
		local ClicksTimer = player:FindFirstChild("ClicksTimer")

		if not ClicksTimer then
			local timer = CreateTimer(player)
			Clicks.Value += addedClicks

			connection = task.spawn(function()
				while timer and timer.Parent do
					task.wait(1)
					Clicks.Value += addedClicks
				end
			end)
		end
	end
end

local function OnTouchEnded(otherPart)
	if connection then
		task.cancel(connection)
		connection = nil
	end
end

part.Touched:Connect(OnTouched)
part.TouchEnded:Connect(OnTouched)

That script still dosent fix when i step off the part it should stop giving you clicks.

I am notsqvid, add me and I can look.

Are you global6s? because im not sure what one

whover global is the fixed it but when you stand on the part it gives you an extra 100 for one second any one knows how to fix that?

Also is there anyway to upgrade the AFK Grinder because I want a upgrade in the gem shop where if you spend gems it will upgrade the AFK grinder.

@global6s why have you unfriended me?

1 Like

Oh oops forgot something:

local part = script.Parent

local addedClicks = 100

local connection = nil

local function CreateTimer(player)
	local timer = Instance.new("BoolValue")
	timer.Name = "ClicksTimer"
	timer.Parent = player

	return timer
end

local function OnTouched(otherPart)
	local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)

	if player then
		local Clicks = player.Data.PlayerData.Currency
		local ClicksTimer = player:FindFirstChild("ClicksTimer")

		if not ClicksTimer then
			local timer = CreateTimer(player)
			Clicks.Value += addedClicks

			connection = task.spawn(function()
				while timer and timer.Parent do
					task.wait(1)
					Clicks.Value += addedClicks
				end
			end)
		end
	end
end

local function OnTouchEnded(otherPart)
	if connection then
		task.cancel(connection)
		connection = nil
	end
end

part.Touched:Connect(OnTouched)
part.TouchEnded:Connect(OnTouchEnded)

Just use Part.TouchEnded:Connect()

For example

local Part = workspace.Part
local GiveCurrency = false
local Currency = {player}.Data.PlayerData.Currency

--functions
function OnTouch()
    GiveCurrency = true
end)

function OnTouchEnded()
    GiveCurrency = false
end)

--

while task.wait() do
   if GiveCurrency == true then
       Currency.Value += 1
   end
end

--

Part.Touched:Connect(OnTouch)

Part.TouchEnded:Connect(OnTouchEnded)

Other parts besides your part can fire the TouchEnded signal. You have to check if the part that began the touch is the same as the one that’s ending the touch.

Hey @everyone anyone willing to enter my roblox game to fix some errors. first the AFK Multiplier is for everyone i think and second adding a gem upgrade for the AFK Multiplier?

Could’nt you just use TouchEnded function?