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)
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’
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
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?