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

How do i make this so there is only a single SoundPart and multiple BeamParts?

Asked by 9 years ago
---- Tele Functions
function Teleport(Player, To) -- Used to Beam Player From Pad
print(Player.Name)
print(To)
if Player and To then
    print"l"
    local S = Player.Humanoid.WalkSpeed
    Player.Humanoid.WalkSpeed = 0
    local l = Player:GetChildren()
    Player.Humanoid.Sit = false
    Player.Torso.Anchored = true
    Player.Head.face.Transparency = 1
    for i = 1, 10 do
        for i = 1, 10 do
            wait()
            local B = script.Parent.BeamPart:Clone()
            B.Parent = script.Parent.Thingies
            B.Anchored = false
            local T = Player.Torso.Position
            local x = T.X + math.random(-1,1)
            local y = T.Y + math.random(-1.4,0)
            local z = T.Z + math.random(-1,1)
            B.CFrame = CFrame.new(x,y,z)
        end 
        local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
            B.BeamSound:Play()
            local T = Player.Torso.Position
            local x = T.X + math.random(0,0)
            local y = T.Y + math.random(0,0)
            local z = T.Z + math.random(0,0)
            B.CFrame = CFrame.new(x,y,z)        
        for i = 1,#l do
            if l[i].ClassName == "Part" then
                l[i].Transparency = l[i].Transparency + 0.1
                end
            if l[i].ClassName == "Hat" then
                l[i].Handle.Transparency = l[i].Handle.Transparency + 0.1
            end
            end
        end
        wait(5)
        Player.Torso.CFrame = To
        wait(5)
    for i = 1, 10 do
        for i = 1, 10 do
            wait()
            local B = script.Parent.BeamPart:Clone()
            B.Parent = script.Parent.Thingies
            B.Anchored = false
            local T = Player.Torso.Position
            local x = T.X + math.random(-1,1)
            local y = T.Y + math.random(-1.4,0)
            local z = T.Z + math.random(-1,1)
            B.CFrame = CFrame.new(x,y,z)
        end 
        local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
            B.BeamSound:Play()
            local T = Player.Torso.Position
            local x = T.X + math.random(0,0)
            local y = T.Y + math.random(0,0)
            local z = T.Z + math.random(0,0)
            B.CFrame = CFrame.new(x,y,z)            
        for i = 1,#l do
            if l[i].ClassName == "Part" then
                l[i].Transparency = l[i].Transparency - 0.1
                end
            if l[i].ClassName == "Hat" then
                l[i].Handle.Transparency = l[i].Handle.Transparency - 0.1
            end
            end
        end
    for i = 1,#l do
        if l[i].ClassName == "Part" then
            l[i].Transparency = 0
            end
        if l[i].ClassName == "Hat" then
            l[i].Handle.Transparency = 0
        end

    end
    Player.HumanoidRootPart.Transparency = 1
    Player.Humanoid.WalkSpeed = S
    Player.Torso.Anchored = false
    Player.Head.face.Transparency = 0
    end
end

0
Umm.. can you shorten this any? :L Nobody wants to read a script that long. Can you pin point the function for us? MessorAdmin 598 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First, tab your code correctly.

A bunch of issues:

  • Use meaningful variable names. What on earth does l mean?

  • You have a for loop over i that has two for loops over i in it. What you're trying to do? I don't know. Only the final loop actually uses i.

  • math.random only takes integer arguments. If you want continuous numbers, use math.random() * (high - low) + low

  • Use generic for loops when you only use your index as list[index].

  • Use :IsA("BasePart") instead of .ClassName == "Part"

  • Use elseif whenever you can

Other suggestions:

Use vectors instead of components. It's much cleaner, clearer, and simpler.

offset = Vector3.new(
    math.random() *2 - 1, math.random() * -1.4, math.random() * 2 - 1)
B.CFrame = CFrame.new( T + offset)

Actually, because you use the pattern math.random() * (high - low) +low so many times, you might want to make a function for it:

function rand(low, high)
    return (high - low) * math.random() + low;
end

......

offset = Vector3.new( rand(-1,1), rand(-1.4, 0), rand(-1, 1) )
B.CFrame = CFrame.new(T + offset)

After applying these improvements, I get this:

function rand(low, high)
    return (high - low) * math.random() + low;
end

function Teleport(Player, To) -- Used to Beam Player From Pad
    print(Player.Name)
    print(To)
    if Player and To then
        local S = Player.Humanoid.WalkSpeed
        Player.Humanoid.WalkSpeed = 0
        local l = Player:GetChildren()
        Player.Humanoid.Sit = false
        Player.Torso.Anchored = true
        Player.Head.face.Transparency = 1
        for repetitions = 1, 10 do
            for count = 1, 10 do
                wait()
                local B = script.Parent.BeamPart:Clone()
                B.Parent = script.Parent.Thingies
                B.Anchored = false
                local T = Player.Torso.Position
                local offset = Vector3.new(rand(-1, 1), rand(-1.4, 0), rand(-1, 1))
                B.CFrame = CFrame.new(T + offset)
            end 
            local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
            B.BeamSound:Play()
            local T = Player.Torso.Position
            B.CFrame = CFrame.new(T)
            for _, part in pairs(l) do
                if part:IsA("BasePart") then
                    part.Transparency = part.Transparency + 0.1
                elseif part:IsA("Hat") then
                    part.Handle.Transparency = part.Handle.Transparency + 0.1
                end
            end
        end
        wait(5)
        Player.Torso.CFrame = To
        wait(5)
        for repetitions = 1, 10 do
            for count = 1, 10 do
                wait()
                local B = script.Parent.BeamPart:Clone()
                B.Parent = script.Parent.Thingies
                B.Anchored = false
                local T = Player.Torso.Position
                local offset = Vector3.new( rand(-1, 1), rand(-1.4, 0), rand(-1, 1) )
                B.CFrame = CFrame.new(T + offset)
            end 
            local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
            B.BeamSound:Play()
            local T = Player.Torso.Position
            B.CFrame = CFrame.new(T)
            for _, part in pairs(l) do
                if part:IsA("BasePart") then
                    part.Transparency = part.Transparency - 0.1
                elseif part.ClassName == "Hat" then
                    part.Handle.Transparency = part.Handle.Transparency - 0.1
                end
            end
        end
        for _, part in pairs(l) do
            if part:IsA("BasePart") then
                part.Transparency = 0
            elseif part:IsA("Hat") then
                part.Handle.Transparency = 0
            end
        end
        Player.HumanoidRootPart.Transparency = 1
        Player.Humanoid.WalkSpeed = S
        Player.Torso.Anchored = false
        Player.Head.face.Transparency = 0
    end
end

This is still not very good.

We don't use repetitions. Instead you're setting the parts transparency based on their previous transparencies. Let's change that:

            for transparency = 1, 10 do
                .....
                if part:IsA("BasePart") then
                    part.Transparency = transparency * 0.1

Notice that the code is repeating itself.

E.g.

            for count = 1, 10 do
                wait()
                local B = script.Parent.BeamPart:Clone()
                B.Parent = script.Parent.Thingies
                B.Anchored = false
                local T = Player.Torso.Position
                local offset = Vector3.new( rand(-1, 1), rand(-1.4, 0), rand(-1, 1) )
                B.CFrame = CFrame.new(T + offset)
            end 

appears exactly twice.

So let's make that a function:

function beamParticles(part)
    for count = 1, 10 do
        wait()
        local B = script.Parent.BeamPart:Clone()
        B.Parent = script.Parent.Thingies
        B.Anchored = false
        local T = part.Position
        local offset = Vector3.new(rand(-1, 1), rand(-1.4, 0), rand(-1, 1))
        B.CFrame = CFrame.new(T + offset)
    end 
end

We have a loop that looks like this:

            for _, part in pairs(l) do
                if part:IsA("BasePart") then
                    part.Transparency = transparency * 0.1
                elseif part:IsA("Hat") then
                    part.Handle.Transparency = transparency * 0.1
                end
            end

That happens three times. So again, make it a function:

function setTransparency(model, transparency)
    for _, child in pairs(model:GetChildren()) do
        if child:IsA("BasePart") then
            child.Transparency = transparency
        elseif child:IsA("Hat") then
            child.Handle.Transparency = transparency
        end
    end
end
-- This also lets us eliminate the variable called "l"

Here's something I missed earlier:

            B.CFrame = Player.Torso.CFrame
            -- Replaces these lines:
            --local T = Player.Torso.Position
            --B.CFrame = CFrame.new(T)

You reference player.Humanoid and player.Torso a bunch, so we should probably make those variables.

Now the result is 30% shorter than the original, and a lot cleaner and simpler:

---- Tele Functions

function rand(low, high)
    return (high - low) * math.random() + low;
end

function beamParticles(part)
    for count = 1, 10 do
        wait()
        local B = script.Parent.BeamPart:Clone()
        B.Parent = script.Parent.Thingies
        B.Anchored = false
        local T = part.Position
        local offset = Vector3.new(rand(-1, 1), rand(-1.4, 0), rand(-1, 1))
        B.CFrame = CFrame.new(T + offset)
    end 
end

function setTransparency(model, transparency)
    for _, child in pairs(model:GetChildren()) do
        if child:IsA("BasePart") then
            child.Transparency = transparency
        elseif child:IsA("Hat") then
            child.Handle.Transparency = transparency
        end
    end
end

function Teleport(Player, To) -- Used to Beam Player From Pad
    print(Player.Name)
    print(To)
    if Player and To then
        local humanoid = Player.Humanoid
        local torso = player.Torso
        local originalWalkSpeed = humanoid.WalkSpeed
        humanoid.WalkSpeed = 0
        humanoid.Sit = false
        torso.Anchored = true
        Player.Head.face.Transparency = 1
        for transparency = 1, 10 do
            beamParticles(torso)
            local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
        end
        B.BeamSound:Play()
        B.CFrame = torso.CFrame
        setTransparency(Player, transparency * 0.1)
        wait(5)
        Player.Torso.CFrame = To
        wait(5)
        for transparency = 10, 0, -1 do
            beamParticles(torso)
            local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
            B.BeamSound:Play()
            B.CFrame = torso.CFrame
            setTransparency(Player, transparency * 0.1)
        end
        setTransparency(Player, 0)
        Player.HumanoidRootPart.Transparency = 1
        humanoid.WalkSpeed = originalWalkSpeed
        torso.Anchored = false
        Player.Head.face.Transparency = 0
    end
end

Now it's pretty clear how to make it not repeatedly make new sounds.

Just lift the creation of the sound object out of the loop. e.g.

.....
            local B = script.Parent.SoundPart:Clone()
            B.Parent = script.Parent.SoundThing
            B.Anchored = true
        end
        B.BeamSound:Play()
        B.CFrame = torso.CFrame
        setTransparency(Player, transparency * 0.1)
        wait(5)
        Player.Torso.CFrame = To
.....
0
Broke the script BearKranz 41 — 9y
Ad

Answer this question