Trying to make a cash giving button that fires an event for the duration of time the player/npc is touching the part. so say a button is supposed to give 100 cash a second and a player stands on the button for 10 seconds they are supposed to get 1000 cash but only get 300-500 depending on how many times the player touched the button walking into in. how do i keep a function firing until the player/npc stops touching the part?
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
local npc = hit.Parent
local maxWood = npc.Configuration.MaxWood
local wood = npc.Configuration.Wood
while wood.Value < maxWood.Value do
wood.Value = wood.Value +1
wait(2)
return
end
end
end)

3 comments
Instead of using a touched event, use region3 to check if a player is in a certain space in the workspace. It's much more reliable for what you're doing.
hi, someone moderated your question about the coins. to answer your question, you should be using a local script instead of a normal scripts. this will make the coin be visible/invisible locally.
a local script wouldn't work with the npc which is the idea behind the question as the npc is server side not client