Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

My Script Quits Working After The First Touch, Help?

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)

4 comments

1

because -25 > -26, it's closer to 0, so it goes to the next if statement and then debounce doesn't become false?

0

I don't know, its a hot mess now.

0

I don't actually understand debounce all that much. If you couldn't tell.

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.

1 answer

✓ Accepted answer

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.

1 comment

0

It definitely helped but now, after the players money gets below -25 and then back above it, the script quits working.

This discussion is preserved as it was. New answers, comments, and votes are closed.