I want to make AFK area to grind money

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? I want to give 1 money who afk on the area per 10 secound

  2. What is the issue? the part is adding value so many.

while true do
	
	wait(10)
	
	script.Parent.Touched:Connect(function(Player)
		
		local humanoid = Player.Parent:FindFirstChild("Humanoid")
		
		if humanoid then
			
			local Plr = game.Players:GetPlayerFromCharacter(humanoid.Parent)
			
			print(Plr)
			
			local leaderstats = Plr:FindFirstChild("leaderstats")
			
			
			local Money = leaderstats:GetChildren()
			
			
			Money[1].Value = Money[1].Value + 1
			
		end
		
	end)
	
end

there is no error code.

also, explorer is like this.
スクリーンショット 2023-08-05 125248

1 Like

I mean you are making a new touched event every 10 seconds so uh yea… thats gonna casue some issues. Because .Touched doesn’t fire actively to who is touching it, but rather fires a lot, so you need a debounce. Instead do something like…

while true do
	
	task.wait(10)
	local GaveMoney = {}
	for i, v in script.Parent:GetTouchingParts() do 
		
		if game.Players:GetPlayerFromCharacter(v.Parent) and table.find(GaveMoney, game.Players:GetPlayerFromCharacter(v.Parent).Name) == nil then
			table.insert(GaveMoney, game.Players:GetPlayerFromCharacter(v.Parent).Name)
			local Plr = game.Players:GetPlayerFromCharacter(v.Parent)
			
			print(Plr)
			
			local leaderstats = Plr:FindFirstChild("leaderstats")
			
			
			local Money = leaderstats:GetChildren()
			
			
			Money[1].Value = Money[1].Value + 1
			
		end
		
	end
	
table.clear(GaveMoney)
end
2 Likes

first of all, you’re making a touched event in a while loop. If you still want to do this, you should instead use script.Parent.Touched:Once(function(Player), which will automatically disconnect it when triggered, but I reccommend just using a touched statement with a debounce if you want a better-ish system

1 Like

Question is, do you want to give money every 10 seconds relative to the start of the server, or every 10 seconds relative to when each player joins the area? Here are ways to implement each. As a note, the relative to player option requires the use of a coroutine to keep the thread running.

10 seconds relative to server:

llocal playersInArea = {}

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			playersInArea[player.UserId] = true
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local character = hit.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	if player then
		playersInArea[player.UserId] = nil
	end
end)

while true do
	task.wait(10)
	for userId, _ in pairs(playersInArea) do
		local player = game.Players:GetPlayerByUserId(userId) -- Get the player object using the user ID
		if player then
			local leaderstats = player:FindFirstChild("leaderstats")
			if leaderstats then
				local Money = leaderstats:FindFirstChild("Money")
				if Money then
					Money.Value = Money.Value + 1
				end
			end
		end
	end
end

10 seconds relative to each player:

local playersInArea = {}

local function addMoney(player)
	while playersInArea[player.UserId] do
		task.wait(10)
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local Money = leaderstats:FindFirstChild("Money")
			if Money then
				Money.Value = Money.Value + 1
			end
		end
	end
end

script.Parent.Touched:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			if not playersInArea[player.UserId] then
				playersInArea[player.UserId] = true
				coroutine.wrap(addMoney)(player)
			end
		end
	end
end)

script.Parent.TouchEnded:Connect(function(hit)
	local character = hit.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	
	if humanoid then
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			playersInArea[player.UserId] = nil
		end
	end
end)

If you have any questions about how each script works or more details, let me know!

3 Likes

Thank you very much!!

I undertand that I could use :touchendded and make a boolen that detect player is in the area or not.
:smile:

2 Likes

Yup, its the check if the player is not already in the table inside of touch connect that acts as the initializer the first connect, then serves as a barrier the next connections like a debounce.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.