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

Can someone make it so the player doesnt get unlimited points?

What happens now is the output says: ' Infinite yield possible on 'Players.Voideyz.Backpack:WaitForChild("Hamburger")'' What it does it gives me way more then 100 points. Can someone put a debounce on this?

script.Parent.Touched:connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
			player.Backpack:WaitForChild("Hamburger")
	if hit.Parent:FindFirstChild("Humanoid") then        
			player.leaderstats.Candy.Value = player.leaderstats.Candy.Value + 100
			local hamburger = player.Backpack.Hamburger			
			script.Sound:Play()
			hamburger:Destroy()
			script.Parent:Destroy()

	
end
end)

		

2 answers

✓ Accepted answer
local enabled = true -- Debounce

script.Parent.Touched:connect(function(hit)
	if not enabled then return end -- Check for debounce
	enabled = false
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.Backpack:WaitForChild("Hamburger") --[[

I suggest replacing the "WaitForChild" for "FindFirstChild", since the first will yield the function indefinitely if the player doesn't own a Hamburger.

]]-- 
    if hit.Parent:FindFirstChild("Humanoid") then        
            player.leaderstats.Candy.Value = player.leaderstats.Candy.Value + 100
            local hamburger = player.Backpack.Hamburger         
            script.Sound:Play()
            hamburger:Destroy()
            script.Parent:Destroy()
	wait(1) -- Change it to the amount of sec you wish to have as cooldown
	enabled = true
end
end)

Your script should be good to go, however I would suggest a couple modifications. As it is now, it will only give points to players who have their Hamburger tool Unequipped. Also the debounce will trigger regardless of the player actually owning a Hamburger or not (thus disabling the button for everyone for the given cooldown). Here is the script adapted to my suggested modifications:

local enabled = true -- Debounce

script.Parent.Touched:connect(function(hit)
	if not enabled then return end -- Check for debounce
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if player.Backpack:FindFirstChild("Hamburger") or hit.Parent:FindFirstChild("Hamburger") then -- Check for hamburger

		if not hit.Parent:FindFirstChild("Humanoid") then return end -- Check for humanoid
		enabled = false
		player.leaderstats.Candy.Value = player.leaderstats.Candy.Value + 100
		local hamburger = player.Backpack.Hamburger         
		script.Sound:Play()
		hamburger:Destroy()
		script.Parent:Destroy()
		wait(1) -- Change it to the amount of sec you wish to have as cooldown
		enabled = true
	end
end)

Infinite yield possible is put in the output when :WaitForChild() has waited a long time. It means that the instance that it is waiting for might never come. If it does eventually arrive, then the script will go on as normal.

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if debounce then return end
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end
	local h = player:WaitForChild("Backpack"):WaitForChild("Hamburger")
	local l = player:WaitForChild("leaderstats"):WaitForChild("Candy")
	debounce = true
	l.Value = l.Value + 100
	script.Sound:Play()
	hamburger:Destroy()
	script.Parent:Destroy() -- We dont need to put "debounce = false" since the button and script are both gone.
end)

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