Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

My Script Quits Working After The First Touch, Help?

Asked by 5 years ago
Edited by Goulstem 5 years ago

The script quits working after the first touch and I have no idea why. Please help, been stuck trying to figure this out for hours (I'm thinking it has to do with my placement of debounce? Not sure) No one has helped yet!

moneyToGive = -25
debounce = false

script.Parent.Touched:connect(function(hit)
    if debounce == true then return end
    player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player == nil then return end
    stat = player:findFirstChild("leaderstats")
    if stat == nil then return end
    cash = stat:findFirstChild("Cash")
    if cash == nil then return end
    debounce = true
    if cash.Value <= -26 then
        script.Sound2:Play()
        debounce = false
    end
    if cash.Value >= -25 then
        cash.Value = cash.Value + moneyToGive
        game.Workspace.Desperado.Body.Hay.Transparency = 0
        script.Sound:Play()
        game.Workspace.Desperado.Body.Cash20l.BillboardGui.Enabled = true
        wait(3.5)
        game.Workspace.Desperado.Body.Cash20l.BillboardGui.Enabled = false
        wait(2)
    end
end)
1
because -25 > -26, it's closer to 0, so it goes to the next if statement and then debounce doesn't become false? DaCrazyDev 444 — 5y
0
I don't know, its a hot mess now. Darkcraft10 20 — 5y
0
I don't actually understand debounce all that much. If you couldn't tell. Darkcraft10 20 — 5y
0
Line 5 cancels the function if debounce is true. You set debounce to true on line 12 but only set it back to false under only one if statement. hellmatic 1523 — 5y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
5 years ago
Edited 5 years ago

You neglect to toggle the debounce variable with that second condition on line 17. Just do it outside any if statements.

local body = workspace.Desperado.Body
local hay = body.Hay
local billboard = body.Cash20l.BillboardGui
local sound1,sound2 = script.Sound,script.Sound2
local moneyToGive = -25
local debounce = false

script.Parent.Touched:Connect(function(hit)
    if debounce then return end
    debounce = true
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if not player then return end
    local stat = player:FindFirstChild("leaderstats")
    if not stat then return end
    local cash = stat:FindFirstChild("Cash")
    if not cash then return end
    if cash.Value <= -26 then
        sound2:Play()
    elseif cash.Value >= -25 then
        cash.Value = cash.Value + moneyToGive
        hay.Transparency = 0
        sound:Play()
        billboard.Enabled = true
        wait(3.5)
        billboard.Enabled = false
        wait(2)
    end
    debounce = false --outside if statements
end)

Also note:

  • connect and findFirstChild are deprecated, use Connect and FindFirstChild.
  • Comparing to nil isn't necessary, use the not operator.
  • Take advantage of variables more. It will clean up your code.
0
It definitely helped but now, after the players money gets below -25 and then back above it, the script quits working. Darkcraft10 20 — 5y
Ad

Answer this question