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)
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)
connect
and findFirstChild
are deprecated, use Connect
and FindFirstChild
.not
operator.