Touched Event Help

Hi,

I want to create a system where a player earns +1 money every second while touching a specific part. The script is parented to the part. And Also How can I stop giving money when the player is no longer touching the part?

Thanks for any help!

With touched events.

local Part = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = plr.leaderstats
	local moneyValue = leaderstats.Money

	Part.Touched:Connect(function(otherPart)
		if otherPart.Parent:FindFirstChild("Humanoid") then
		moneyValue.Value += 1		
		end
	end)
end)

Heres what I wrote, im not sure if it works, but it should.

You could use GetPartsInBox, or GetPartsInPart and keep it inside of a while loop.

Basically, you could filter it to the HumanoidRootPart, and inside of that while loop, you could give +1 money to whosever character is inside. Here’s an example:

local Players = game:GetService("Players")

local PlayersToCheck = {} -- Will add every players character in here

for i, plr : Player in Players do
	table.insert(PlayersToCheck, plr.Character)
end

Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	
	table.insert(PlayersToCheck, plr.Character)
end)

Players.PlayerRemoving:Connect(function(plr)
	local char = table.find(PlayersToCheck, plr.Character)
	if char then
		table.remove(PlayersToCheck, char)
	end
end)

local checkPart = script.Parent

local partCFrame = checkPart.CFrame

local radius = checkPart.Size + Vector3.new(0,10,0)


while true do
	local filter = OverlapParams.new()
	filter.FilterType = Enum.RaycastFilterType.Include
	
	for i, char in PlayersToCheck do
		filter:AddToFilter(char.HumanoidRootPart) -- Adding the hrp to the filter (so that only those are checked)
	end
	
	local playersInArea = workspace:GetPartBoundsInBox(partCFrame, radius, filter) -- Getting list of all players in box
	
	for i, hrp in playersInArea do
		local player = Players:GetPlayerFromCharacter(hrp.Parent)
		if player then -- Double checking to see if player
			player.leaderstats.Money.Value += 1 -- Will increase their value
		end
	end
	task.wait(1) -- Will do this each second
end

Really wasn’t supposed to go into that much detail, but it is what is. I feel like everything that I didn’t comment on was self explanatory.