Problem with pressing button

Hey guys, I am beginner scripter, I have problem with my script, so there is a button and when a player touches it, the player should get let’s say 10$ but there is a problem that when a player touches the button the button would stop in just few second, but I don’t want to make the button to stop randomly. I want it to go on forever (until player stops touching, he button).

here is my script where player would get 10$ every time he touches the button but after just 2-3 seconds it stops detecting the player and then it stops. Anyway to fix it?

local playerService = game:GetService("Players")
local button = script.Parent.redbutton

local bounce = false
local function ontouched(hit)
	if bounce then return end
	bounce = true
	local character = hit.Parent
	local player = playerService:GetPlayerFromCharacter(character)

	if player then
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if cash then
				local pass2x = player.ButtonsFolder.Cash2x.Value
				local pass3x = player.ButtonsFolder.Cash3x.Value
				local pass5x = player.ButtonsFolder.Cash5x.Value

				cash.Value = cash.Value + 10 
				print(cash.Value)
				
			end
		end
	end
	wait(0.1)
	bounce = false
end



button.Touched:Connect(function(playertouch)
	ontouched(playertouch)
end)

now it stops!

1 Like

A quick thing to try is add a new part, make it 3d so its the same shape as that red base but make it so it comes up to a bit taller than the player. Then make can collide = False and transparency = 1. Then add that new part into your script as your new ‘button’

A bit like this:

It just means if the players moving about it is less glitchy.

Yeah so if let’s say the player is AFK and he goes to a button will it still generate cash or it will stop?

Use GetTouchingParts() instead in a loop as it gets all the touching parts all time, meaning it will work even if player is AFK in button.

local playerService = game:GetService("Players")
local button = script.Parent.redbutton

local bounce = false
local function onTouched(hit)
	local character = hit.Parent
	local player = playerService:GetPlayerFromCharacter(character)

	if player then
		bounce = true
		local leaderstats = player:FindFirstChild("leaderstats")
		if leaderstats then
			local cash = leaderstats:FindFirstChild("Cash")
			if cash and not bounce then
				bounce = true
				local pass2x = player.ButtonsFolder.Cash2x.Value
				local pass3x = player.ButtonsFolder.Cash3x.Value
				local pass5x = player.ButtonsFolder.Cash5x.Value

				cash.Value = cash.Value + 10 
				print(cash.Value)
				
				task.wait(0.1)
				bounce = false
			end
		end
	end
end

while true do
	for _, part in ipairs(button:GetTouchingParts()) do
		task.spawn(onTouched, part)
	end
	
	task.wait()
end

hey what if i need to make it only exclusive if a player is VIP?

what do you mean? i dont gets what you mean

what if i want it that if a player wants to AFK then he would need to buy a gamepass or smth like that? Can i do it?

like if the player afk in the button he would still get cash? because players have the free will to afk

Umm ok leave that but i have one another problem when a player touches a button so let’s say that button would give him 1$ and he purchase another button (let’s say button2 for $1000 ) but when he touches the button2 the cash value goes to 1000 again rather than him getting 10$ per touch?

but you said this

i thought that’s what you wanted but ok

yeah it is fixed so screw that So can you resolve this also I am struggling too. No force you can resolve this if you have free time

1 Like

also try to avoid reposting the same problem like what you did here

because it will consider as spamming

No this is a different problem! this one is different and the post I gave is different!

ooh, sorry for misunderstanding :sweat_smile:

No Problem, Mistake can Happen! :slight_smile:

1 Like

Hey sadly it does not work out, i can’t press it!