Value inventory problem (update function)

1.I want to create a value inventory for my role-playing game. If there is 1 illegal tree (value), there should be a box with this item. If there are 0 illegal trees, the UI should disappear

  1. Sadly there are two issues so far:
    a) If the player has only one item, the entire system glitches (UI text is no longer updated)
    2022-03-14 22-27-13

b) If a player joins the game with only 1 item, nothing will appear.

  1. a) I have no clue what’s wrong with it.
    b) I tried to use onplayeradded which was supposed to load information into the user interface after the player joined the game. Unfortunately it doesn’t work.

scripts below:

  1. text update script
local player = game.Players.LocalPlayer

local data = player:WaitForChild("IllegalMoney")

local text1 = game.ReplicatedStorage:WaitForChild("IllegalTree")
local text2 = game.ReplicatedStorage:WaitForChild("IllegalTree_Packaged")

local direct = player.PlayerGui.Inventory.Background.ScrollingFrame

local jest1 = player.PlayerGui.Inventory.Background:WaitForChild("IllegalTreeUI")
local jest2 = player.PlayerGui.Inventory.Background:WaitForChild("IllegalTree_PackagedUI")

local function update()
	direct:WaitForChild("IllegalTree").tekscior.Text = "Illegal Trees: " .. tostring(data.IllegalTree.Value)
	direct:WaitForChild("IllegalTree_Packaged").tekscior.Text = "Packaged Trees: " .. tostring(data.IllegalTree_Packaged.Value)
	
	game.ReplicatedStorage:WaitForChild("IllegalTree").tekscior.Text = "Illegal Trees: " .. tostring(data.IllegalTree.Value)
	game.ReplicatedStorage:WaitForChild("IllegalTree_Packaged").tekscior.Text = "Packaged Trees: " .. tostring(data.IllegalTree.Value)
end

data.IllegalTree:GetPropertyChangedSignal("Value"):Connect(update)
data.IllegalTree_Packaged:GetPropertyChangedSignal("Value"):Connect(update)

update()
	
  1. remove/add box to UI

local data = player:WaitForChild("IllegalMoney")

local text1 = game.ReplicatedStorage:WaitForChild("IllegalTree")
local text2 = game.ReplicatedStorage:WaitForChild("IllegalTree_Packaged")

local direct = player.PlayerGui.Inventory.Background.ScrollingFrame

local jest1 = player.PlayerGui.Inventory.Background:WaitForChild("IllegalTreeUI")
local jest2 = player.PlayerGui.Inventory.Background:WaitForChild("IllegalTree_PackagedUI")



local function update()


if data.IllegalTree.Value >0 and jest1.Value == false then text1:Clone().Parent = direct
	jest1.Value = true

elseif data.IllegalTree.Value == 0 and jest1.Value == true then 
	direct:WaitForChild("IllegalTree"):Destroy()
		jest1.Value = false
		
	end
	
	if  data.IllegalTree_Packaged.Value >0 and jest2.Value == false then text2:Clone().Parent = direct
		jest2.Value = true

	elseif data.IllegalTree_Packaged.Value == 0 and jest2.Value == true then 
		direct:WaitForChild("IllegalTree_Packaged"):Destroy()
		jest2.Value = false
	end

	
	
	end
	
	
	data.IllegalTree:GetPropertyChangedSignal("Value"):Connect(update)
	data.IllegalTree_Packaged:GetPropertyChangedSignal("Value"):Connect(update)


	update()

thanks in advance

Can you do this at the bottom of the second script
game.Players.Playeradded:Connect(function(update)

sorry in advanced I wrote this on mobile