Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help with if/else ParticleEmitter Script?

Asked by 6 years ago

I am attempting to make a script to Enable and disable particles (there are 24 particle emitters) from a part by clicking a nob, but it's not working. Could you please help? Thanks in advance!

function shower()
local nob = script.Parent.HeadandNob.Nob
local head = script.Parent.HeadandNob.HEad
local pe = head.one, head.two, head.three, head.four, head.five, head.six. head.seven. head.eight. head.nine, head.ten, head.eleven, head.twelve, head.thirteen, head.fourteen, head.fifteen, head.sixteen, head.eighteen, head.nineteen, head.twenty, head.twentyone, head.twentytwo, head.twentythree, head.twentyfour
local state = script.Parent.Value

if state.Value == false
then state.Value = true
pe.Enabled = true

else
    state.Value = false
    pe.Enabled = false

    end
end
script.Parent.HeadandNob.Nob.ClickDetector.MouseClick:connect(shower)

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

On line 4, you're defining pe with multiple values. This is what arrays are for. Essentially, lines 9 and 13 are only affecting the first value assigned to the variable. Here's an example of how this works:

local blah = "1",2;
print(blah) --> 1 (only "1" is assigned to 'blah')
local first,second = "1",2;
print(first,second) --> 1 2 (a reference was made for both values)

You can take advantage of arrays here. Put all of the Particles in an array, then iterate through the array and change each index's Properties.

local nob = script.Parent.HeadandNob.Nob
local head = script.Parent.HeadandNob.HEad --Typo?
local pe = {head.one, head.two, head.three, head.four, head.five, head.six. head.seven. head.eight. head.nine, head.ten, head.eleven, head.twelve, head.thirteen, head.fourteen, head.fifteen, head.sixteen, head.eighteen, head.nineteen, head.twenty, head.twentyone, head.twentytwo, head.twentythree, head.twentyfour} --Array
local state = script.Parent.Value

function changeStateTo(bool) --Parameter: true_or_false.
    state.Value = bool;
    for _,v in next,pe do
        v.Enabled = bool;
        wait();
    end
end

function shower()
    if not state.Value then
        changeStateTo(false);
    else
        changeStateTo(true);
    end
end

--`connect` is deprecated.
script.Parent.HeadandNob.Nob.ClickDetector.MouseClick:Connect(shower)
0
Thank you for responding! I am still learning the Lua code so I'm not exactly certain about functions and such. Am I suppose to use this code exactly? Because I tried it and it's not working arkenzii 6 — 6y
0
Yes, this code was supposed to be an exact solution. Functions are ways of defining chunks of code that you want to reuse with differentiated outcomes. You define a function with "parameters", these parameters are called "arguments" when the function is called. So in the example above "bool" on line 6 is equivalent to "false" on line 16, and "true" on line 18. Goulstem 8144 — 6y
0
Do you have any errors in the output? Will help me determine what could be wrong :) Goulstem 8144 — 6y
0
Thank you so much for explaining! Yes, in the console log it says 'head is not a valid member of ParticleEmiter' arkenzii 6 — 6y
View all comments (9 more)
0
Make sure you're indexing the object correctly on line 2 :) Goulstem 8144 — 6y
0
Yupp, head is located in the Shower model in HeadandNob. Is there anything else that could be preventing the script from working? arkenzii 6 — 6y
0
Indexing your hierarchy is case-sensitive so make sure *everything* is spelled correctly. You indexed `HEad` from `script.Parent.HeadandNob` which could have been meant to be `Head` Goulstem 8144 — 6y
0
I checked the capitalization of everything and there was a mistake where I capitalized head when I meant to lowercase it. I changed this in the script and it still doesn't seem to be working arkenzii 6 — 6y
0
Error update? :) They help alot. Goulstem 8144 — 6y
0
Okay, it says 'head is not a valid member of ParticleEmitter' and I don't know if this has something to do with it, but it also says 'startScript re-entrancy has exceeded 3' arkenzii 6 — 6y
0
That's the same error as last time mate are you positive you fixed? Goulstem 8144 — 6y
0
Yeah I rechecked everything, and I still don't know what I'm doing wrong :( arkenzii 6 — 6y
0
I figured it out. I had to take the arrays off and give each pe (Particle Emitter) it's own local arkenzii 6 — 6y
Ad

Answer this question