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

BillboardGui not properly cloning/destroying on true value?

Asked by 6 years ago

Hi guys,

so I've been messing around with this script for a bit now and I'm just so confused! The idea is that when a user joins it creates a BoolValue, changes the parent to the player in game.Players, and goes on false by default.

This all works fine, no issues there, until I get to the part where it should CHECK if the user is wanted by using a while loop.

Here's my code:

game.Players.PlayerAdded:Connect(function(p)

        local bool = Instance.new("BoolValue") --Creates new BoolValue for players
        bool.Parent = p
        bool.Name = "Wanted"
        bool.Value = false

    p.CharacterAdded:Connect(function(char) 

        function checkwanted()
            while wait(1) do
                if bool.Value == true then
                    if char.Head:FindFirstChild("wanted") then
                        char.Head:FindFirstChild("wanted"):Destroy()
                    else
                        local gui = script:FindFirstChild("wanted"):Clone()
                        gui.Parent = char.Head
                        gui.Enabled = true
                    end
                elseif bool.Value == false then
                    if char.Head:FindFirstChild("wanted") then
                    char.Head:FindFirstChild("wanted"):Destroy()
                    end
                end
            end
        end     

        checkwanted()

    end)
end)

Now you might've guessed it already, what I'm trying to do is make sure the loop checks if the user is wanted every second. If he is, it checks if the user already has a GUI. When he does, it removes the GUI to prevent multiple GUIs in the character's head. The issues is, and I am well aware, that the while loop adds and creates a new GUI every second because it detects a GUI already. This is incredibly annoying, and I know no other way to stop this!

Do you guys have any suggestions for me? I cannot seem to figure a way out to make sure it checks if the user has a GUI already and then stops, but STILL checks if the user's value is false to permanently remove the GUI

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago

Hello! It sounds like you could take advantage of the Changed event :)

Rather than using a while loop to check every iteration, run code when the Changed event fires.

local template = Instance.new("BoolValue")
template.Name = "Wanted"

game.Players.PlayerAdded:Connect(function(p)
    local bool = template:Clone()
    bool.Parent = p;
    bool.Changed:Connect(function()
        if bool.Value then
            --If value changed true
        else
            --If value changed false
        end
    end)
end)
Note.. The server doesn't have access to client's PlayerGuis. You're going to have to use FireClient and OnClientEvent on a RemoteEvent object. Find out more here
0
Works like a charm! Much appreciated :) I'll look into those Clients, and the Changed event is a savior. User#20989 0 — 6y
Ad

Answer this question