Cant use loop to keep updating text

So basically i’m trying to make a minimap and let the client see the distance,but idk why it just doenst work,any ideia why? heres my code:

local showdistance=true

local islands = {
    ["Ghost Town"] = Vector3.new(-200, 13, -4600),
    ["Joruto"] = Vector3.new(-7001, 5, -5392),
    ["Maka"] = Vector3.new(-4400, 7, -2500),
    ["Colosseum"] = Vector3.new(-2000, 7, -7700),
    ["Spawn"] = Vector3.new(1060, 9, 1200)
}
        for i,v in next,islands do
            part = Instance.new("Part")
            part.Transparency=1
            part.Name = i
            part.Anchored=true
            part.Position = v
            part.Parent=game.workspace

            local BillboardGui = Instance.new("BillboardGui")
            local TextLabel = Instance.new("TextLabel")
            local UIListLayout = Instance.new("UIListLayout")

            BillboardGui.Parent = part
            BillboardGui.AlwaysOnTop = true
            BillboardGui.LightInfluence = 1
            BillboardGui.Size = UDim2.new(0, 80, 0, 20)
            BillboardGui.StudsOffset = Vector3.new(0, 120, 0)
            UIListLayout.Parent = BillboardGui

            TextLabel.Parent = BillboardGui
            TextLabel.BackgroundTransparency = 1
            TextLabel.Size = UDim2.new(0, 80, 0, 10)
            TextLabel.Text = i
            TextLabel.TextColor3 = Color3.new(1, 1, 1)
            TextLabel.TextSize = 10
            if showdistance==true then
                print("yes")
                TextLabell = Instance.new("TextLabel")
                TextLabell.Parent = BillboardGui
                TextLabell.BackgroundTransparency = 1
                TextLabell.Size = UDim2.new(0, 80, 0, 10)
                TextLabell.TextColor3 = Color3.new(1, 1, 1)
                TextLabell.TextSize = 9
                TextLabell.Position = UDim2.new(0, 0, 0.5, 0)
                local posicaoplayer = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
                local distanciax = posicaoplayer.X - v.X
                local distanciaz = posicaoplayer.Z -v.Z
                print(distanciax)
                print(distanciaz)
                distanciafinal = math.sqrt(distanciax*distanciax+distanciaz*distanciaz)
                print(distanciafinal)
                while true do
                    wait()
                      TextLabell.Text=  tostring(distanciafinal)
                end
                print("working")
            end
        end
        print("test")
        

You’ll need to create a new thread. (i don’t really know how to explain it lol)

coroutine.wrap(function()
while wait() do
TextLabell.Text = tostring(distanciafinal)
end
end)()
1 Like

As @flkfv said, you will need to create a new thread. However, instead of using a coroutine, I recommend using spawn() for simplicity reasons.

local showdistance=true

local islands = {
    ["Ghost Town"] = Vector3.new(-200, 13, -4600),
    ["Joruto"] = Vector3.new(-7001, 5, -5392),
    ["Maka"] = Vector3.new(-4400, 7, -2500),
    ["Colosseum"] = Vector3.new(-2000, 7, -7700),
    ["Spawn"] = Vector3.new(1060, 9, 1200)
}
        for i,v in next,islands do
            part = Instance.new("Part")
            part.Transparency=1
            part.Name = i
            part.Anchored=true
            part.Position = v
            part.Parent=game.workspace

            local BillboardGui = Instance.new("BillboardGui")
            local TextLabel = Instance.new("TextLabel")
            local UIListLayout = Instance.new("UIListLayout")

            BillboardGui.Parent = part
            BillboardGui.AlwaysOnTop = true
            BillboardGui.LightInfluence = 1
            BillboardGui.Size = UDim2.new(0, 80, 0, 20)
            BillboardGui.StudsOffset = Vector3.new(0, 120, 0)
            UIListLayout.Parent = BillboardGui

            TextLabel.Parent = BillboardGui
            TextLabel.BackgroundTransparency = 1
            TextLabel.Size = UDim2.new(0, 80, 0, 10)
            TextLabel.Text = i
            TextLabel.TextColor3 = Color3.new(1, 1, 1)
            TextLabel.TextSize = 10
            if showdistance==true then
                print("yes")
                TextLabell = Instance.new("TextLabel")
                TextLabell.Parent = BillboardGui
                TextLabell.BackgroundTransparency = 1
                TextLabell.Size = UDim2.new(0, 80, 0, 10)
                TextLabell.TextColor3 = Color3.new(1, 1, 1)
                TextLabell.TextSize = 9
                TextLabell.Position = UDim2.new(0, 0, 0.5, 0)
                local posicaoplayer = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
                local distanciax = posicaoplayer.X - v.X
                local distanciaz = posicaoplayer.Z -v.Z
                print(distanciax)
                print(distanciaz)
                distanciafinal = math.sqrt(distanciax*distanciax+distanciaz*distanciaz)
                print(distanciafinal)

                spawn(function()
                    while true do
                        wait()
                          TextLabell.Text=  tostring(distanciafinal)
                    end
                 end)

                print("working")
            end
        end
        print("test")
        

If you scroll down towards the bottom where you created your infinite while loop you’ll see I nested it inside of a spawn() function.

This will basically make it so the loop will run without halting the rest of your script

The reason having just a while loop doesn’t work is because it stops the rest of the items in the for i,v in next, islands loop. It’s like a traffic jam.

Hope this helps! :slight_smile:
~ Xitral, Lua Programmer

2 Likes

Are you a geniuos?? TF thanks bro
also i did that for work:

if showdistance==true then
                spawn(function()
                        local TextLabell = Instance.new("TextLabel")
                        TextLabell.Parent = BillboardGui
                        TextLabell.BackgroundTransparency = 1
                        TextLabell.Size = UDim2.new(0, 80, 0, 10)
                        TextLabell.TextColor3 = Color3.new(1, 1, 1)
                        TextLabell.TextSize = 9
                        TextLabell.Position = UDim2.new(0, 0, 0.5, 0)
                        while true do
                            wait()
                            local posicaoplayer = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
                            local distanciax = posicaoplayer.X - v.X
                            local distanciaz = posicaoplayer.Z -v.Z
                            local distanciafinal = math.sqrt(distanciax*distanciax+distanciaz*distanciaz)
                            TextLabell.Text=  tostring(distanciafinal)
                        end
                end)
            end
1 Like