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 5 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.

local debounce = true
local d = script.Parent.Decal
t = 0.06

function onTouched(hit)
if (debounce == true) then
    debounce = false
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)
    wait(t)
    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)

    script.Parent.CanCollide = false
    script.Parent.Transparency = 1
    d.Parent = nil
    wait(3)
    d.Parent = script.Parent
    script.Parent.CanCollide = true
    script.Parent.Mesh.Scale = Vector3.new(1,1,1)
    script.Parent.Transparency = 0
    debounce = true
end
end

connection = script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
0
Answered by 5 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!

function onTouched(part)
    mesh = script.Parent.Mesh --use more variables !! 

    if debounce == true then -- don’t wrap this in brackets!
        debounce = false
        for i = 1, .05, -.01 do
            mesh.Scale = Vector3.new(
                mesh.Scale.x - .05,
                mesh.Scale.y,
                mesh.Scale.y -.05
                wait(t)
)
        end

        script.Parent.CanCollide = false
        script.Parent.Transparency = 1
        d.Parent = nil
        wait(3)
        script.Parent.CanCollide = true
        script.Parent.Mesh.Scale = Vector3.new(1,1,1)
        script.Parent.Transparency = 0
        debounce = true

    end
end

script.Parent.Touched:Connect(onTouched)
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 — 5y
0
In his line 61 code, and wrapping conditionals in brackets errors due to brackets being math exclusive. User#19524 175 — 5y
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 — 5y
Ad

Answer this question