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

how could I use a single variable for multiple events without interference?

All I'd like to know is how to shorten this into a single variable for each event.

debounce1 = false
debounce2 = false
debounce3 = false

part.Touched:Connect(function()
	debounce1 = true
end)
part2.Touched:Connect(function()
	debounce2 = true
end)
part3.Touched:Connect(function()
	debounce3 = true
end)

This is what I am looking for:

debounce1 = false
part.Touched:Connect(function()
	debounce1 = true
end)
part2.Touched:Connect(function()
	debounce1 = true
end)
part3.Touched:Connect(function()
	debounce1 = true
end)

Although I am trying to figure out how to make it so that debounce1 only changes for that specific event and doesn't interfere with each other.

I've done a bit, tried to make a table to store each one then another table to load it specifically for the event, the problem with that was that the default values couldn't be changed unless it was changed in the event which in that case a loop would make it harder to code.

I tried making a function that returns values but then again, I couldn't change the original values, I tried making it specifically work

I'd like some ideas, thanks for the help and time.

The reason I asked this is because it would make my code neater and easier to read and also, it would reduce the memory consumption.

1 comment

0

What do you mean default values can’t be changed?

3 answers

✓ Accepted answer

#yo bruvs!

i was browsing old posts on scripting helpers and i found that my boy nei posted THIS question.

i was like,,,,,,, what is going on, why hasn't nobody answered NEIL'S question? neil, the important neil,

#NEIL THE GREAT 80!!

i hit him up over discord and i'm like:

"oi neil, what u mean by this??"

he respond, crying for help:

i want to give each function their own debounce without having to manually write out each variable pls help :(

so i told him we can use tables to keep track of the debounce: currentAnimations["variableName"] creates a new key in the dictionary currentAnimations which we set as true/false. this acts as a debounce without having to worry about running out of upvalues like we would if we were using variables like in op's question.

currentAnimations = {}

function fireBall()
  if not currentAnimations["Fireball"] then 
    currentAnimations["Fireball"] = true
    do fireball stuff....
    wait(cooldown)
    currentAnimations["Fireball"] = false
  end
end


function tank()
  if not currentAnimations["Tank"] then 
    currentAnimations["Tank"] = true
    do Tank stuff....
    wait(cooldown)
    currentAnimations["Tank"] = false
  end
end

22 comments

1

And it works 100%

0

test

0

test

0

test

0

test

0

test

0

test

0

test

0

test

0

test

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

0

test2

NOTE: Not 100% Sure If This Works & Is Efficient Of What You're Looking For

I re-thought of the table thought you had. If you use 3 tables one with false and one with true and one with nil, because why not, you can use the same debounce variable, but for different events.

Script Hierarchy

Module Script

local globalDebounce = {}

globalDebounce.TrueDebounce = true

globalDebounce.FalseDebounce = false

globalDebounce.NilDebounce = nil

return globalDebounce

ServerScript

local debounceModule = require(script.ModuleScript)
local trueDebounce = debounceModule.TrueDebounce
local falseDebounce = debounceModule.FalseDebounce
local nilDebounce = debounceModule.NilDebounce
local debounce

part.Touched:Connect(function()
	debounce = trueDebounce
	print(debounce)
end)

part2.Touched:Connect(function()
	debounce = falseDebounce
	print(debounce)
end)

part3.Touched:Connect(function()
	debounce = nilDebounce
	print(debounce)
end)

I'm trying my best, hope this is somewhat of what you're looking for or atleast giving you an idea of how to handle this situation.

~killerbrenden

13 comments

0

it will constantly change if I call the event, that won't work.

0

I'm not 100% sure if they allow these functions to be enacted on. But again, I'm not 100% sure.

0

I am using a module, _G is a bad method and this won't be helpful because I am trying to localize a single variable outside of an event.

0

I just can't figure out how though.

0

Not sure if you can, because if you change it in a ModuleScript, it'll change in all the other events too.

0

Since you were talking about tables, all I could think about is to have 2 tables inside of the module, one with Debounce to true and one to False, but that doesn't seem efficient.

0

Yeah that will double the memory consumption for each variable in each table.

0

Yeah, I edited the code. That doesn't seem efficient as it takes up more game memory. :/

0

Buddy, using three tables will be worse, that is like creating a table for each variable and once I hit like.. 50 variables because that is how many power ups are going to exist in my module, the code will be insanely inefficient and i'd have to redo the entire script.

0

Yeah, I realized that after I read your comment.

0

Sorry, not that much of a help. Hope I could of at least gave you somewhat of an idea.

0

Eh, you tried, thank you for your time and effort.

I can think of two methods to go about this, one just syntactical difference and one with different logic. Either way, premature optimisation isn’t a good idea, you’re decreasing your productivity at the expense of negligible effects. Worry about optimisation that has major effects (concerning physics, etc.), not variable assignment.

Single line, multiple assignments


Just assign all variables one the same line, it makes it neater since like variables are assigned on the same line, which imposes a degree of separation:


local debounce1, debounce2, debounce3 = false, false,false

-- You can make this even shorter if you check for truthy values instead of just false:
local debounce1, debounce2, debounce3

part.Touched:Connect(function()
    debounce1 = true
end)
part2.Touched:Connect(function()
    debounce2 = true
end)
part3.Touched:Connect(function()
    debounce3 = true
end)

Tables and truthy checks


Since all keys in a table are given a value of nil by default, you don’t need to initialise any values and you can have one variable, but, you’ll have to use specific keys in specific events:


local debounce = {}

part.Touched:Connect(function()
    debounce[part1] = true -- You don’t need to map my instances but it’s more intuitive
end)
part2.Touched:Connect(function()
    debounce[part2] = true
end)
part3.Touched:Connect(function()
    debounce[part3]= true
end)

2 comments

0

Premature optimisation could be quite useful in my case by simplifying my code which would allow me to program faster, I just found out a way to do this, also I tried tables and indexes.

0

It isn’t useful, since you’re spending time on it for no apparent benefit. Including this time you’re spending on this question.

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