Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Variable doesn't update with connects?

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

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

1 comment

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. :/

This discussion is preserved as it was. New answers, comments, and votes are closed.