UI not popping up after setting newCharacter [Solved]

My solution
local function SoulTreeGUI()
	return  player.PlayerGui:WaitForChild("SoulTreeGUI")
end

local function toggleSoulTree()
	local SoulTreeGUI = SoulTreeGUI()
	local soulTreeCache = player:FindFirstChild("SoulTreeCache")
	if not soulTreeCache then
		local soulTreeCache = Instance.new("Folder")
		soulTreeCache.Name = "SoulTreeCache"
		soulTreeCache.Parent = player
	end
	
	if SoulTreeGUI.Enabled == false then
		SoulTreeGUI.Enabled = true
		SoulTreeGUI.SoulTree.Visible = true
	elseif SoulTreeGUI.Enabled == true then
		SoulTreeGUI.Enabled = false
		SoulTreeGUI.SoulTree.Visible = false
	end
	if #player.SoulTreeCache:GetChildren() ~= 0 then
		print("show save and exit pop up thing")
	end
end
ContextActionService:BindAction("SoulTreeGUI", function(_, UserInputState)
	if UserInputState == Enum.UserInputState.Begin then
		toggleSoulTree()
	end
end, false, toggleSoulTreeKeyBind)

I am just calling a function to grab the soulTreeGUI to get the soulTreeGUI after setting the players character to a new character. I was wondering if there is a way around this or if it is bad practice/performant to do it this way.
(this is a local script in player scripts, other player scripts work fine atm)

    -- Code to show new character ... other code not needed to show
	player.Character = newCharacter
	newCharacter.Parent = workspace	

The character does not affect the state of the gui. So you do not need to reset the Gui. Also try changing the character on the server

I am changing the character on the server.

Previous Code
local soulTreeGUI = player.PlayerGui:WaitForChild("SoulTreeGUI")
local function toggleSoulTree()
	local soulTreeCache = player:FindFirstChild("SoulTreeCache")
	if not soulTreeCache then
		local soulTreeCache = Instance.new("Folder")
		soulTreeCache.Name = "SoulTreeCache"
		soulTreeCache.Parent = player
	end
	
	if soulTreeGUI.Enabled == false then
		soulTreeGUI.Enabled = true
		soulTreeGUI.SoulTree.Visible = true
		print (soulTreeGUI.Enabled, soulTreeGUI.SoulTree.Visible)
	elseif soulTreeGUI.Enabled == true then
		soulTreeGUI.Enabled = false
		soulTreeGUI.SoulTree.Visible = false
	end
	if #player.SoulTreeCache:GetChildren() ~= 0 then
		print("show save and exit pop up thing")
	end
end
ContextActionService:BindAction("SoulTreeGUI", function(_, UserInputState)
	if UserInputState == Enum.UserInputState.Begin then
		toggleSoulTree()
	end
end, false, toggleSoulTreeKeyBind)

Also when I put these print statements in before it was printing true for both even though they were false in the explorer on the client

Error logs for previous code

19:16:09.231 true true - Client - SoulTreeUI:20
19:16:11.098 SoulTree is not a valid member of ScreenGui “SoulTreeGUI” - Client - SoulTreeUI:19
19:16:11.099 Stack Begin - Studio
19:16:11.099 Script ‘Players.PryVexx.PlayerScripts.UIHandlers.SoulTreeUI’, Line 19 - function toggleSoulTree - Studio - SoulTreeUI:19
19:16:11.099 Script ‘Players.PryVexx.PlayerScripts.UIHandlers.SoulTreeUI’, Line 31 - Studio - SoulTreeUI:31
19:16:11.099 Stack End - Studio
19:16:11.099 ContextActionService: Unexpected error while invoking callback: SoulTree is not a valid member of ScreenGui “SoulTreeGUI” - Studio

Check if SoulTree exists in the ScreenGui. If it does, then add a :WaitForChild()

If it’s disabled on spawn then check off ResetOnSpawn, because resetting (setting again) the character will cause all ScreenGuis with this property enabled to reset. Without deeper understanding in how your code works, I’ve just provided a solution to the simplest scenario

Turing “ResetOnSpawn” to false fixed it. Thank you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.