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

why is the first loop only running and not the others, im so confused??

Asked by 3 years ago

In my game i need to tween a lot of billboardGui's size when you're close to them. I made this script to handle them all, but the first one is working not the others, do you possibly need to use a coroutine? I've heard they might be related to this problem. Can someone give a brief answer explaining in detail?

local Player = game.Players.LocalPlayer
local CheckItem = require(script.CheckItem)

local Char = Player.CharacterAdded:Wait()

local TweenItem = require(script.TweenItem)

function checkDist(part1, part2)
    if typeof(part1) ~= Vector3 then part1 = part1.Position end
    if typeof(part2) ~= Vector3 then part2 = part2.Position end

    return (part1 - part2).Magnitude
end

local function tweenStuffOut(item)
    if CheckItem.Check(item) == true then
        local Expandation = TweenItem.changeSize(1, 1, item)

        Expandation:Play(

        )
    end
end

local function tweenStuffIn(item)
    if CheckItem.Check(item) == true then
        local Expandation = TweenItem.changeSize(0, 0, item)

        Expandation:Play()
    end
end


local function Run(realItem)
    while wait() do
            if checkDist(Char.HumanoidRootPart, realItem.Parent.Parent) <= 20 then
            print("close")
        tweenStuffOut(realItem)
    else
        tweenStuffIn(realItem)

        end
    end 
end

-- Main Billboards

Run(workspace.Applications.BankShop.Step.BillboardGui.TextLabel)

Run(workspace.Applications.WeaponShop.StepPart.BillboardGui.TextLabel)

Run(workspace.Applications.PetShop.Step.BillboardGui.TextLabel)

Run(workspace.Applications.DailyGroupReward.StepPart.BillboardGui.TextLabel)



0
Both of the answers work, i don't know which thing to pick. But I've heard spawn(function() is deprecated. I'll accept raid6n's answer, but I'll upvote tizzel40's answer :D CaIcuIati0n 246 — 3y

2 answers

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

I would use coroutine.wrap.

https://developer.roblox.com/en-us/articles/Beginners-Guide-to-Coroutines

local Run =  coroutine.wrap(function(realItem)
        while wait() do
            if checkDist(Char.HumanoidRootPart, realItem.Parent.Parent) <= 20 then
                print("close")
                tweenStuffOut(realItem)
            else
                tweenStuffIn(realItem)
            end
        end
    end
)
Ad
Log in to vote
1
Answered by
Tizzel40 243 Moderation Voter
3 years ago

The Reason Why The First loop is only running is because , when you run a loop inside the code , It yeilds , or 'stops' the rest of the script until the Loop has been completed , stopped or broken by using the keyword break inside the loop

The Answer to your problem is using coroutines or spawn

they both run code on different threads , meaning it will not yield or stop the rest of the code

I usally use the spawn function mostly too.

So basically , what you can do is for every line that has the run() function just put this in

spawn(function()---Will Run on a sperate thread and will not yeild the other parts over the script !
Run(workspace.Applications.BankShop.Step.BillboardGui.TextLabel)
end)

---/ Run your next function
spawn(function()

end)

with this technique , you can run multiple functions and things at one time ! :D !

Have a good day lol ! :D !

-----T40

Answer this question