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

How do I make a single script that manages multiple parts in my game?

Asked by 6 years ago

I have a script that shrinks a part that the player touches. Instead of having to insert the same script in hundreds of different parts (Which creates lag) my goal is to have one single script control all the parts with a certain name (i.e ShrinkingPart) to shrink. This is the script for each part.

01local debounce = true
02local d = script.Parent.Decal
03t = 0.06
04 
05function onTouched(hit)
06if (debounce == true) then
07    debounce = false
08    wait(t)
09    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
10    wait(t)
11    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
12    wait(t)
13    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
14    wait(t)
15    script.Parent.Mesh.Scale = Vector3.new(script.Parent.Mesh.Scale.x-0.05,script.Parent.Mesh.Scale.y,script.Parent.Mesh.Scale.z-0.05)
View all 61 lines...

1 answer

Log in to vote
0
Answered by 6 years ago

You’re wrapping your if statement in brackets, and using deprecated code.

And Dude! You don’t need so many lines!! Use a for loop!

01function onTouched(part)
02    mesh = script.Parent.Mesh --use more variables !!
03 
04    if debounce == true then -- don’t wrap this in brackets!
05        debounce = false
06        for i = 1, .05, -.01 do
07            mesh.Scale = Vector3.new(
08                mesh.Scale.x - .05,
09                mesh.Scale.y,
10                mesh.Scale.y -.05
11                wait(t)
12)
13        end
14 
15        script.Parent.CanCollide = false
View all 27 lines...
0
Where is there use of deprecated elements? Is there anything inherently wrong with wrapping conditionals in brackets? Also I don't think this actually answers the question being asked..? fredfishy 833 — 6y
0
In his line 61 code, and wrapping conditionals in brackets errors due to brackets being math exclusive. User#19524 175 — 6y
0
You should probably point out specifically what's deprecated, rather than just assuming people will notice one character out of 61 lines changing. Also, wrapping conditionals in brackets is valid syntax in Lua: https://i.imgur.com/L68ZHRB.png fredfishy 833 — 6y
Ad

Answer this question