How can I "bypass" variables by using a different method?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to code more efficiently so i want to bypass as much variables as possible. It’s a bit complicated to explain but i can try by using this example:

How I would write it currently:

local exampleboolvalue = false
local examplepart = workspace.examplepart

examplepart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if exampleboolvalue == false then --to make it execute just once!
        exampleboolvalue = true
        --code to be executed
        end
    end
end)

How I would like to write it:

local examplepart = workspace.examplepart

examplepart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if -a boolvalue just fo this ifstatement which isnt a variable- == false then --to make it execute just once!

        --code to be executed
        end
    end
end)
  1. What is the issue? Include screenshots / videos if possible!

I have absolutely no idea how to do this because im not the best in coding.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to search for someone who asked the same question but i dont know how to search for it because its hard for me to explain it in a simple question.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

If you don’t want the .touched event to ever fire again you can :Disconnect it by storing it as a variable then later in the code to be executed you disconnect it so it doesnt get called again.

local examplepart = workspace.examplepart

local connection = examplepart.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        

        --code to be executed
        connection:Disconnect()
    end
end)
1 Like

Okay thanks that seems to be a good method.