Script didnt work after player respawn

i made a setting that players can hide the game’s GUI with the setting,but it won’t work anymore after player respawn(its only work on some guis that the ResetOnSpawn is false), I didn’t open the ResetOnSpawn for the setting, but some other GUI is on, so how do i do that i can still let this work after player reset.

so basically it only work on some gui that the ResetOnSpawn is false, after player respawn

here is the setting’s script

local player = game.Players.LocalPlayer
local menus = player.PlayerGui:GetDescendants()
local guibuttons = player.PlayerGui:WaitForChild("GuiButtons"):GetDescendants()
local sound = game.SoundService["Click Sound"]
local clicked = false

script.Parent.MouseButton1Down:Connect(function()
	sound:Play()
	if not clicked then
		clicked = true
		for i,v in pairs(menus) do
			if v:IsA("ScreenGui") and v:FindFirstChild("Gui") then
				print(v)
				v.Enabled = false
				script.Parent.UIStroke.Transparency = 0.4
				script.Parent.ImageTransparency = 0.5
			end
		end
		for i,v in pairs(guibuttons) do
			if v:IsA("ImageButton") and v.Name ~= "SettingBtn" then
				v.Visible = false
			end
			if v:IsA("ImageButton") and v.Name == "SettingBtn" then
				v.BackgroundTransparency = 0.9
				v.UIStroke.Transparency = 0.9
				v.ImageTransparency = 0.8
			end
		end
	else
		clicked = false 
		for i,v in pairs(menus) do
			if v:IsA("ScreenGui") and v:FindFirstChild("Gui") then
				v.Enabled = true
				script.Parent.UIStroke.Transparency = 0.75
				script.Parent.ImageTransparency = 0.7
			end
		end
		for i,v in pairs(guibuttons) do
			if v:IsA("ImageButton") and v.Name ~= "SettingBtn" then
				v.Visible = true
			end
			if v:IsA("ImageButton") and v.Name == "SettingBtn" then
				v.BackgroundTransparency = 0.3
				v.UIStroke.Transparency = 0.5
				v.ImageTransparency = 0.4
			end
		end
	end
end)

errors

1 Like

This is because your gui is destroyed and a new one is made when the character is added. This is explained in the documentation of PlayerGui.
The reason it errors is because your script does not reset because it is not inside the PlayerGui. Meaning all references to the gui still refer to the old gui that was destroyed when the player died.

You can either update the references to the new gui, put your LocalScript inside the gui so that is is also destroyed and added back together with the gui or turn off ResetOnRespawn

3 Likes

“local menus = player.PlayerGui:GetDescendants()” You’re getting the descendants, and if I remember right that means you are getting ALL of the things inside the playergui, that includes menus, instances inside the menu and so on, so probably you are getting an instance that isn’t the thing you want

1 Like

It is inside the player GUI

“Players.BIBIMO233.PlayerGui.SettingMenu2.Gui.GuiHide.LocalScript”

It’s probably the script that she writed up there on the post, and as you can see, it is indeed inside the PlayerGui, and doesn’t Gui reset when you die?

2 Likes

You are right, how could I not see that.
Then the problem is probably that the ResetOnRespawn of the ScreenGui the script is in is false and the others are not causing everything to reset accept the ScreenGui the local script is it resulting in this unexpected behaviour.

2 Likes

Oh right! That actually makes alot of sense, and now I realized that the fault isn’t the Descendants since it checks for its type, so you’re most likely right. Nice thinking!

2 Likes

I split the settings menu into two parts like this
屏幕截图 2024-09-17 082636
i turn on the ResetOnSpawn of the SettingMenu2 and its work now,i know it may not be the best solution.

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