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)
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 )
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