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

How do I turn on and off a particle emitter?

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!

5 comments

0

Sorry not all the code went in.

0

Instead of not having the enabled value be a variable, Instead just have the emitter itself be a variable and do "rocket.Enabled = false" cause sometimes Roblox lua can be a little weird when it comes to variable values and full on objects

0

rocked.Enabled = false

0

That is because OP does not know the difference between a reference and a value, I'm typing up an answer right now

0

Thank you! <3

1 answer

####The formatting of the code is screwed, read here for information on how to format your questions/answers.

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.

#Reference vs Value It is important that you know the difference between a reference and a value.

##Reference

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.

##Value 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.

#Final product

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)

######Hopefully this answered your question, and if it did, then don't forget to hit that "Accept Answer" button. If you have any other questions, then feel free to leave them down in the comments.

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