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

How to create a function that keeps track of the number of items left?

My goal is to create a short script that kills a player after all the required parts have been destroyed. This is done by stepping on a trigger (Trigger2) to destroy all parts. Now, the part of the code I have at line 33 does end up killing my character, but that's simply because it makes sense (Because 3 minus 3 does equal to 0. In this case, that would be "myarray" - "myarray" = "emptytable") . Instead, I want to create a function that keeps track of the remaining parts (Touch1, Touch2, and Touch3), but I don't know how to.

local Touch1 = game.Workspace.Touch1
local Touch2 = game.Workspace.Touch2
local Touch3 = game.Workspace.Touch3
local player = game.Players.LocalPlayer
local myarray = {workspace.Touch1, workspace.Touch2, workspace.Touch3}
local emptytable = {}
debounce = false

function message()
	local m = Instance.new("Message", workspace)
	m.Text = "Touch the trigger to die"
	wait(2.5)
	m:remove()
			
	
end

message()


function OnTouched()
	if debounce == true then return end
	debounce = true
	
	for i,v in pairs(myarray) do
		Touch1:destroy()
		wait()
		Touch2:destroy()
		wait()
		Touch3:destroy()
	end	
	
	**if #myarray - 3 == #emptytable then -- this is the part that I don't like. Want to change**
		game.Players.LocalPlayer.Character.Humanoid:TakeDamage(100)
	end
end

game.Workspace.Trigger2.Touched:connect(OnTouched)


1 comment

0

thanks so much.

1 answer

✓ Accepted answer

First, I noticed that your usage of generic iterators was a bit odd. You have all the values hard-coded into the loop, which is exactly what the generic for loop is supposed to prevent. A generic for loop with the pairs/ipairs function is used for iterating over all elements of a table, without knowing how many there are in it. It will automatically break once all elements have been iterated over. The element currently being iterated over is stored in the second variable, in this case called v.

Therefore, since you're destroying every part in the table, there is no need to check the length afterwards.

local Touch1 = game.Workspace.Touch1
local Touch2 = game.Workspace.Touch2
local Touch3 = game.Workspace.Touch3
local player = game.Players.LocalPlayer
local myarray = {workspace.Touch1, workspace.Touch2, workspace.Touch3}
local emptytable = {}
debounce = false

function message()
    local m = Instance.new("Message", workspace)
    m.Text = "Touch the trigger to die"
    wait(2.5)
    m:Destroy()
end

message()

function OnTouched()
    if debounce == true then return end
    debounce = true

    for i,v in pairs(myarray) do
        v:Destroy()
        wait()
    end

    game.Players.LocalPlayer.Character.Humanoid:TakeDamage(100)

    debounce = false -- I also noticed that you never reset the debounce.
end

game.Workspace.Trigger2.Touched:connect(OnTouched)

Hopefully I understood the question correctly.

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