Problem with a function

So, i have 2 problems.

  1. i want a function (part.touched:…) to be executed once, when i want.

  2. I want to make a loop wait until the function executes.

my code simplified

local table1 = {"A","B","C"}

function()
 local triggerfunction = false
 for i,v in pairs(table1) do
  if v == "A" then
  triggerfunction = true
  -- i want to wait with this loop until "Part" touches something
  end
 end

end

if tiggerfunction then
  -- i want this function to only trigger once
  part.touched:Connect(function() end)
end

local triggeredOnce: boolean = false

part.Touched:Connect(function()
   if triggeredOnce then return end
   triggeredOnce = true
end

could you explain to me what : boolean = false means?

I also need to make the timer/wait thingy.

Boolean just means true or false, I add the : boolean there personally when writing my scripts. Also what times, I don’t understand your question?

function()
 local triggerfunction = false
 for i,v in pairs(table1) do
  if v == "A" then
  triggerfunction = true
  -- i want to wait with this loop until "Part" touches something. Also how to do this?
  end
 end

end

You could just do something like this, since I still don’t understand what you mean.

local touchedConnection

local function Disconnect()
   if touchedConnection then
      touchedConnection:Disconnect()
      touchedConneetion = nil
   end
end

local function Connect()
   touchedConnection = part.Touched:Connect(function()
      Disconnect()
   end
end

Connect() -- Whenever you want to connect the function you would use Connect()

what i want, is for the loop (for in do end) to wait, and only continue when

part.touched:Connect(function() end)

is triggerd.

Oh I think I get what your saying

local triggered = false

part.Touched:Connect(function()
   triggered = true
end

local Table = {"A", "B", "C"}
for _, String in ipairs(Table) do
   if not triggered then repeat task.wait() until triggered end
   -- Do whatever you want here
end

oh wait. i realized i wrote my “example codewrong”

its suppost to be

local table1 = {"A","B","C"}

function()
 local triggerfunction = false
 for i,v in pairs(table1) do
  if v == "A" then
  triggerfunction = true
  -- i want to wait with this loop until "Part" touches something
  end
 end
 if tiggerfunction then
  -- i want this function to only trigger once
  part.touched:Connect(function() end)
 end
end

it doesn’t work in here. should i place it inside the loop?

1 Like

No it shouldn’t be in the loop. My example is how it should be. Though I don’t get what this needed for exactly?

Than how should i do this.

(yeah i asked a lot of “weird questions” lately. I need this for a project. And its not eazy to explain it.)

Well, the code I sent you should do what you need it to. You just have to modify it for your own game.

2 Likes

: is used for typechecking.

local var: number = 5

var = "string" -- Type `string` could not be converted into `number`
local function test(var: number)
	return var + 10
end

test("string") -- Type `string` could not be converted into `number`
local numbers: {number} = {5, 6, 10}

table.insert(numbers, "string") -- Type `string` could not be converted into `number`

It acts as a powerful linting tool where you can specify what type a variable is. You can add this in variables, function parameters, tables, and other things. It also autocompletes specific properties or functions that you can use based on what type annotation you specified.

1 Like

Well, placing it above (and inside the func) doesn’t work, placing it under the loop (still in the func) doesn’t work. I don’t know where to place the

part.Touched:Connect(function()
   triggered = true
end

Since .Touched is an RBXScriptSignal, you can use an in-built function (:Wait()) that specifically yields until that signal has been fired. You can read more about signals here.

function()
	local triggerfunction = false
	
	for i,v in pairs(table1) do
		if v == "A" then
			triggerfunction = true
			part.Touched:Wait()
			
			warn(".touched has been fired!")
		end
	end
end

Wait, this could be very usefull. let me check if it works

1 Like