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

Variable doesn't update with connects?

Asked by
lomo0987 250 Moderation Voter
9 years ago

What i'm trying to do is edit a variable so when a child is added/removed it will connect a function..

wait(3)
player = game.Players.LocalPlayer

function Party()
    this = nil
    this = game.Lighting.Party:FindFirstChild(player.InParty.Value)
end

function update()
    Party()
    print(this)
    local Slots = script.Parent:GetChildren()
    local ok = this:GetChildren()
    wait(1)
    for i = 1, #Slots do
        for a = 1, #ok do
            if i == a then
                Slots[i].Text = ok[a].Name
            end
        end
    end
end

function deupdate()
    local retext = script.Parent:GetChildren()
    for i = 1, #retext do
        if retext[i]:IsA("TextLabel") then
            retext[i].Text = ""
        end
    end
    update()

end

update()
Party()

this.ChildAdded:connect(deupdate)
this.ChildRemoved:connect(deupdate)

in function Party it's meant to update the variable "this" It only works with the original variable. Do I also need to update the connects at the bottom of the script?

1 answer

Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
9 years ago

Yes you also need to update the connects at the bottom. Also, you should declare the this variable beforehand.

wait(3)
local player = game.Players.LocalPlayer
local this
local update
local deupdate

function Party()
    this = game.Lighting.Party:FindFirstChild(player.InParty.Value)
    this.ChildAdded:connect(deupdate)
    this.ChildRemoved:connect(deupdate)
end

function update()
    Party()
    print(this)
    local Slots = script.Parent:GetChildren()
    local ok = this:GetChildren()
    wait(1)
    for i = 1, #Slots do
        for a = 1, #ok do
            if i == a then
                Slots[i].Text = ok[a].Name
            end
        end
    end
end

function deupdate()
    local retext = script.Parent:GetChildren()
    for i = 1, #retext do
        if retext[i]:IsA("TextLabel") then
            retext[i].Text = ""
        end
    end
    update()
end

update()
Party()

0
That doesn't work. It just keeps adding to the pile. and if a child gets added/removed it will fire multiple times so that doesn't work. :/ lomo0987 250 — 9y
Ad

Answer this question