Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to make cash giver button keep giving cash until the player stops touching?

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

0

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.

0

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.

0

a local script wouldn't work with the npc which is the idea behind the question as the npc is server side not client

1 answer


local touching = false
local npc

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then
	touching = true --Tells the game they are touching it using a boolean
	npc = hit.Parent
        end
    end)

script.Parent.TouchedEnded:Connect(function(hit)
    if hit.Parent:FindFirstChildOfClass("Humanoid") then
	touching = false--Tells the game they are NOT touching it using a boolean
        	
        end
    end)

while touching = true do
	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

Hope this works!

3 comments

0

You missed an end, in the last part

0

Syntax error on line 18

0

no error after fixing the end and == issue but the value is not increasing

This discussion is preserved as it was. New answers, comments, and votes are closed.