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

Why Is It Not Adding A Clone Of The Gui If It Does Not Find It In PlayerGui?

When the player joins the server, the player has a gui in the PlayerGui area.

HOWEVER... when the player resets, that gui is removed. So I want this script to automatically see if the player has that gui in PlayerGui, and add it into PlayerGui if it is not there. But... I can not get it to work.

Coding For The Script That Adds The Gui If It Does Not Find It In PlayerGui:

Please Note: This Is A LocalScript

--//LocalScript

repeat 
	wait(0.1)
	local Player = game.Players.LocalPlayer
	if Player.PlayerGui:FindFirstChild("MenuGUI") then
		--Do Nothing If MenuGUI Is Already In PlayerGui
	else
		game.Workspace.MenuGUI:Clone().Parent = Player.PlayerGui --If It Does Not Find A "MenuGUI" In The PlayerGui, It Should Automatically Clone MenuGUI From Workspace And Place The Clone Into PlayerGui
	end
until Player == nil

1 answer

✓ Accepted answer

Your current code will only run until Player is nil, there are better solution to this problem which do not involve loops.

Possible solution 1

If you are using the StarterGui then you can set the property ResetPlayerGuiOnSpawn which sets "Whether or not the PlayerGui should reset upon the respawning of the player. "

Possible solution 2

If you would like to use a new gui each time then you could put a script into StarterCharacterScripts as these scripts get parented in a player's Character upon a respawn.

Example:-

-- it is a good idea to put the gui into ReplicatedStorage  
local repStorage = game:GetService('ReplicatedStorage'):WaitForChild('some gui name')
repStorage:Clone().Parent = game.Players.LocalPlayer:WaitForChild('Backpack') -- clone the gui
script:Destroy() -- remove the script once we have the gui

I hope this help.

1 comment

0

Thank You So Much! :D

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