Stop giving currency when player has stopped touching 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.

This looks like it works, let me know if this one fails.

This one belongs inside of the AFK Part.

local AFKPart = script.Parent

local function onPartTouched(partThatTouched)
	local touchEnded
	if partThatTouched.Name == "HumanoidRootPart" then
		local player = game:GetService("Players"):GetPlayerFromCharacter(partThatTouched.Parent)
		local afk = player.AFK
		if afk.Value == false then
			afk.Value = true
			touchEnded = AFKPart.TouchEnded:Connect(function(partThatEnded)
				if partThatTouched == partThatEnded then
					afk.Value = false
					touchEnded:Disconnect()
				end
			end)
		end
	end
end

AFKPart.Touched:Connect(onPartTouched)

This one goes in ServerScriptService

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 deb = false

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

Unfortunately it dosent give me clicks when i touch the afk part

Do you receive any errors when touching the part?

Yes i got this
13:59:20.756 AFK is not a valid member of Player “Players.planeboy2021” - Server - AFKEarner:7
and this
13:58:28.169 Touched is not a valid member of ServerScriptService “ServerScriptService” - Server - Script:20

Wrong scripts in the wrong spots. Swap them out. That’s my fault too…

I think both scripts are completely the same

Wait you’re right check the edit

Nope still dosent work and i got this error
14:08:54.793 Data is not a valid member of Player “Players.planeboy2021” - Server - Script:6

Take the code I gave you for the server and put it inside of the script where you create leaderstats.

After those values are created. Remove the local clicks = …

I have a folder called Datastore should i put it in there? Also in that folder is all the datastore scripts

What do you mean by the local clicks?

game.Players.PlayerAdded:Connect(function(player) -- Remove this
	local afk = Instance.new("BoolValue")
	afk.Name = "AFK"
	afk.Parent = player
	
	local clicks = player.Data.PlayerData.Currency -- This line here. 
	local gain = 100
	local cooldown = 1
	local deb = false

	afk:GetPropertyChangedSignal("Value"):Connect(function()
		if afk.Value and deb == false then
			deb = true
			repeat
				task.wait(cooldown)
				if afk.Value then
					clicks.Value += gain -- Change clicks.Value to the name of the currency inside that script.
				end
			until not afk.Value
			deb = false
		end
	end)
end)

Which script to I put that in?