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

How can I activate a function with a loop for multiple parts at once?

Asked by 4 years ago

I'm working on a silent hill sort of game and I'm struggling to activate the transition from the Fog World to the Otherworld. I store every part of the transition in one folder for easier access for the script. Each transitioning parts have a Particle Emitter named "P" which needs to be activate first and all at once, and then a loop to make the parts transparent all at the same time. Finally turn off the emitters at the same time.

So far I tried to two things, both equally bad.

local TransitionParts = game:GetService("Workspace")["Transition parts"]:GetChildren()

local function Transition()
    local g = 0
    print("Activated")
    wait(1)
    warn("Creepy Siren Noises")
    wait(3)

        --First attempt
    --[[
    for i,v in pairs(TransitionParts) do
        v.P.Enabled = true
        for g = 0, 1, .1 do
            v.Transparency = g
            print(g)
            wait(0.35)
        end
        v.P.Enabled = false
    end--]]


    --Second attempt
    for _,v in pairs(TransitionParts) do
        v.P.Enabled = true
    end
    for _,v in pairs(TransitionParts) do
        g = g + 1
        TransitionParts.Transparency = g --Doesn't even work
        print(g)
        wait(0.35)
    end
    for _,v in pairs(TransitionParts) do
        v.P.Enabled = false
    end 

    print("Complete")
end

script.Parent.ClickDetector.MouseClick:Connect(Transition)

I know its messy, I'm trying to get it to work before I can clean it up some.

At first I tried doing it all in one loop but did one part at a time, then tried 3 seperate loops but for whatever reason the transparency change isn't working nor can the 3rd loop find P.

I'm probably missing something really simple and just over thinking it.

0
Do you want to change the transparency of every part at the same time? Or one at a time. EpicMetatableMoment 1444 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

I am assuming you want to change the transparency of every part at the same time. I would suggest using TweenService since it's :Play() method does not yield the current thread.

Solution 1: Tween Service

https://developer.roblox.com/ko-kr/api-reference/class/TweenService

https://developer.roblox.com/ko-kr/api-reference/function/TweenService/Create

If you would want to enable all P particles, smoothly interpolate the transparency, and then after that has finished disable all P particles you could do something like this:

local TweenService = game:GetService("TweenService")

--// [Insert rest of your code here]

local function Transition()

    for _, v in pairs(TransitionParts) do
        v.P.Enabled = true
    end

    for _, v in pairs(TransitionParts) do
        --// create a new Tween
        local Tween = TweenService:Create(v, TweenInfo.new(.35), {
            Transparency = 1
        })

        Tween:Play() --// play the tween's animation
    end
    wait(.35) --// wait .35 seconds for all tweens to finish 

    for _, v in pairs(TransitionParts) do
        v.P.Enabled = false
    end 
end

--// [Insert rest of your code here]

Solution 2: Coroutine or Spawn

I don't suggest using these so I am just going to gloss over them briefly.

Alternatively you could use the spawn function, or the coroutine library. I would say both work in this scenario, but I would prefer to use TweenService.

The spawn function works like coroutine.wrap but automatically calls your passed function.

https://www.lua.org/pil/9.1.html

The coroutine library is meant to make Lua, from the front end, appear to be multi-threaded. Lua is realistically not a multi-threaded language but can "act" as if it were allowing several lines of code to "run at the same time."

The coroutine library offers two functions that would probably help you now.

  • coroutine.wrap
  • coroutine.create
1
I'll see what I can do with TweenService. I didn't know it didn't yield the thread. Even though I have worked with it a time or two. BlauKitten 88 — 4y
0
If this solved your problem, do mark it as answer! EpicMetatableMoment 1444 — 4y
1
Tested it and it worked beautifully! Thank you! BlauKitten 88 — 4y
Ad

Answer this question