Hello Everyone! I am A beginner in scripting or noob in scripting, So I got a Problem!
before I tell you, let’s see the main parts for this incident:
button 1:
generates around 1$ per touch.
so, I want that when a player touches the button, he would get 1$ per touch Forever! (until he steps out) but here is the problem when the player touches the part for example if I touch it rather than getting 1$ continuously, I get 1$ for continuously for around 3-4 second and then it Justs Stops! yeah so just imagine if a player wants to AFK on the button but he can’t so that is the problem.
Can you resolve this issue? because I tried so many times but it didn’t work out!
here is the script!
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)
you’re using a bounce variable to prevent multiple touches in quick succession, but this approach is causing the button to stop giving cash after a short delay.
to make the button continuously give cash as long as the player is touching it, you should use a different approach. You can use a while loop to keep adding cash while the player is touching the button, and use the Humanoid.Touched event instead of Touched to detect the player’s touch.
try using this method:
local playerService = game:GetService("Players")
local button = script.Parent.redbutton
local function onHumanoidTouched(humanoid)
local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
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
while humanoid:IsDescendantOf(button) do
cash.Value = cash.Value + 1 -- add $1 continuously
wait(1)
end
end
end
end
end
button.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
onHumanoidTouched(humanoid)
end
end)
You can loop ‘give money’ to a player until they leave the button.
local isTouching = false
local function loopGive()
while isTouching do
--Give money
end
end
part.Touched:Connect(function(hit)
isTouching = true
loopGive()
end)
part.TouchEnded:Connect(function(hit)
isTouching = false
end)
Hey you did not understand. everything is working properly but when the player is stepping on the button it should give them money but when they step on it and after stepping on it just 2-3 seconds it stops even thought there are above the button.
the script you showed is when a player steps on the button he gets money and when left he does not gets any money!
You can add a invisible noncollidable part above the button. I’ve made buttons before, and my issue was the animation so I had to add an extra part just above the button.
This is a good example I found: