Answered by
7 years ago Edited 7 years ago
DanzLua is correct, but that isn't your only problem -- you also called onClick
when trying to give MouseClick
something to connect to. Since onClick doesn't return anything, you effectively told it to connect to nil
(or "nothing"). It evaluates like this:
1 | script.Parent.ClickDetector.MouseClick:connect(onClick()) |
3 | script.Parent.ClickDetector.MouseClick:connect( nil ) |
To see such errors in the future, be sure to have the Output window open. (The Script Analysis window is also very helpful - if it displays a warning, there is almost always something wrong that you should fix; it can also catch syntax errors before you run the script.)
Below, I show how you might use a function to ensure that you needn't copy/paste code:
01 | local stage 1 = game.Workspace.Stage 1 Water:GetChildren() |
02 | local stage 2 = game.Workspace.Stage 2 Water:GetChildren() |
03 | local stage 3 = game.Workspace.Stage 3 Water:GetChildren() |
04 | function setTransparency(list, transparency) |
06 | if list [ i ] :IsA( "BasePart" ) then |
07 | list [ i ] .Transparency = transparency |
12 | if script.Parent.OnOff.Value = = false then |
13 | script.Parent.OnOff.Value = true |
14 | setTransparency(stage 1 , . 55 ) |
16 | setTransparency(stage 2 , . 55 ) |
18 | setTransparency(stage 3 , . 55 ) |
20 | script.Parent.OnOff.Value = false |
21 | setTransparency(stage 1 , 1 ) |
22 | setTransparency(stage 2 , 1 ) |
23 | setTransparency(stage 3 , 1 ) |
26 | script.Parent.ClickDetector.MouseClick:Connect(onClick) |
Note:
- I've put DanzLua's code in the function
setTransparency
- as you can see, it's much more efficient than copy pasting the for
loop 6 times (instead, we just have to call the function 6 times).
connect
is deprecated, use Connect
instead
- You could put the different
stage
s in a table and then iterate over them with a for loop (but this isn't necessary and doesn't save any code when you only have 3 stages -- but if you ever end up wanting 4+ stages, it's becomes worth it)
There is another problem with your script - it won't prevent someone from clicking it multiple times. This will mess up the animations. ex, say someone clicks it at "t=0" (let that mean "time of 0"), then stage1 will be semi-transparent at t=0 and stage2 will be semi-transparent at t=10 -- but say someone activates the ClickDetector at t=15 -- then everything will become transparent immediately (which is fine), but your script will then make stage3 semi-transparent at t=20.
There are two fixes:
- Use "debounce". Players will not be able to interrupt the animation (so they won't be able to turn it off until it's fully turned on).
- Use "call numbers". Players will be able to interrupt the animation at any time. (Whether this is a good thing depends entirely on what you want to do.)
Since you can look up "debounce" examples, I will show you the 2nd option. New script:
01 | local stage 1 = game.Workspace.Stage 1 Water:GetChildren() |
02 | local stage 2 = game.Workspace.Stage 2 Water:GetChildren() |
03 | local stage 3 = game.Workspace.Stage 3 Water:GetChildren() |
04 | function setTransparency(list, transparency) |
06 | if list [ i ] :IsA( "BasePart" ) then |
07 | list [ i ] .Transparency = transparency |
13 | local thisNumber = callNumber |
14 | callNumber = callNumber + 1 |
15 | if script.Parent.OnOff.Value = = false then |
16 | script.Parent.OnOff.Value = true |
17 | setTransparency(stage 1 , . 55 ) |
19 | if callNumber ~ = thisNumber then return end |
20 | setTransparency(stage 2 , . 55 ) |
22 | if callNumber ~ = thisNumber then return end |
23 | setTransparency(stage 3 , . 55 ) |
25 | script.Parent.OnOff.Value = false |
26 | setTransparency(stage 1 , 1 ) |
27 | setTransparency(stage 2 , 1 ) |
28 | setTransparency(stage 3 , 1 ) |
31 | script.Parent.ClickDetector.MouseClick:connect(onClick) |
The new code involves the variables callNumber
and thisNumber
. Quite simply, the function will stop animating if onClick is called during the animation. In the same example I stepped through before, if a user clicks at t=0, pretend callNumber is 3, so thisNumber will also be 3. At t=10, stage2 is semi-transparent (as before). The script then compares callNumber and thisNumber, but doesn't return early because they are still the same. At t=15, the user clicks again and everything becomes semi-transparent -- and callNumber is increased by 1 and is now 4. Thus, at t=20, when the script would normally make stage3 semi-transparent, it instead compares callNumber (4) with thisNumber (still 3) and returns early -- thus, everything remains fully transparent. (In contrast, if you added debounce, the user clicking would do nothing until after t=20, since the animation would still be running.)