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

Why Doesn't My GUI Get Destroyed When the Player is Away From a Part?

Asked by 4 years ago
Edited 4 years ago

I have GUI here that needs to be looked at because what I'm trying to do is make it so if a player is not around any item then the GUI needs to be destroyed but it's not doing that. This piece of code is in a LocalScript if anyone needs to know.

local players = game:GetService("Players")
local collectionService = game:GetService("CollectionService")
local rangeValue = 9
local player = players.LocalPlayer

-- Checks if the player is not around any item and deletes the Collect Gui if they aren't
while wait(0.1) do
    local checkGui = player.PlayerGui:FindFirstChild("CollectItemGui")

    if checkGui then
        local adornee = checkGui.CollectBillBoardGui.Adornee

        if (player.Character.HumanoidRootPart.Position - adornee.Position).Magnitude >  rangeValue then
            checkGui:Destroy()
        end
    end
end

When i run around the item, the GUI most of the time gets destroyed but sometimes it doesn't for some reason and I'm not sure. Is there something I'm forgetting or doing something wrong?

Answer this question