I'm trying to create an on/off button for a particle emitter, but what I tried doesn't work.
local rocket = workspace.fire.ParticleEmitter.Enabled local button = script.Parent
function click() if script.Value.Value then rocket = true
else rocket = false end
end
script.Parent.MouseButton1Click:Connect(click)
Please tell me what I'm doing wrong. Thank you!
The problem here is the fact that you wrongly assume that the rocket
variable is a reference to the Enabled
property of the ParticleEmitter. If and whenever the Enabled property changes, your variable will not update. To solve your issue, you would have the variable hold the ParticleEmitter object itself, and make a direct access to the Enabled
property.
It is important that you know the difference between a reference and a value.
In Lua, for example, tables are passed by reference.
local t1 = {"hello", "world", "foo", "bar"} local t2 = {"hello", "world", "foo", "bar"} local t1ref = t1 table.insert(t1, "test") for _, v in ipairs({t1, t2, t1ref}) do print(table.concat(v, ", ")) end --[[ output: hello, world, foo, bar, test hello, world, foo, bar hello, world, foo, bar, test --]]
t1
and t1ref
both have the same exact table in memory and t2
is a different table even if it has the same values in the same position. Changes made to t1
or t1ref
will affect t1
and t1ref
.
Data like booleans and strings are passed simply by value.
local str = "hello" local str2 = str str = "world" print(str, str2) --> world hello
Despite str2
being set to str
, both variables have different memory and so modifying one value will not modify the other.
local rocket = workspace.fire.ParticleEmitter local button = script.Parent local function click() rocket.Enabled = script.Value.Value -- no need for an if statement, just set enabled to the value end script.Parent.MouseButton1Click:Connect(click)