My GUI wont clone into player after respawn

Hello. I currently have a GUI that shows a variety of classes. Once you select your class, you click a confirm button and the gui is deleted with the code

	script.Parent.Parent.Parent:Destroy()

Then, I have a script that is supposed to clone the fresh copy of this GUI (Located in Server Storage) into the user’s gui each time their character loads in (respawns). The code for that is below.

game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:connect(function(char)
			local ClonedGUI = game.ServerStorage.ClassLoadout:Clone()
			ClonedGUI.Parent = plr.PlayerGui
			print("cloned")
	end)	
end)


The script works perfectly the first time the player is loaded in, but after re spawning, the GUI is no longer cloned into the players gui (can confirm I had the explorer open while testing)
I placed the print “cloned” line to make sure that my code was getting that far, and it was, so is there something I am doing wrong?

Thanks,
migte

3 Likes

Did cloned get printed into the output?

Yes, the cloned message was printed, which in theory means that there was no error stopping the script from functioning.

game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			local ClonedGUI = game.ServerStorage.ClassLoadout:Clone()
			ClonedGUI.Parent = plr.PlayerGui
			print("cloned")
	end)	
end)

Are you sure the GUI is visible? [also try this code, I changed your connect from lower c to C, because lowercase connect was deprecated]

1 Like

Can you send a bit more of the client-sided code that deletes the gui? Also, check explorer to see if the gui actually exists in your PlayerGui.

I’am not sure about the issue, but try those two:

(What sloss has suggested)

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local ClonedGUI = game.ServerStorage.ClassLoadout:Clone()
		ClonedGUI.Parent = plr.PlayerGui
		print("cloned")
	end)	
end)

Or

game.Players.PlayerAdded:Connect(function(plr)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local ClonedGUI = game.ServerStorage.ClassLoadout:Clone()
	ClonedGUI.Parent = plr.PlayerGui
	print("cloned")
end)

Also, i just noticed you have putten 2 :connects() in your/the code.

and that’s on copy and paste, thanks!

1 Like

The gui does not actually exist in the playerGui. It does exist the first time, but after that it is never loaded in, regardless of the code actually working, since “cloned” is printed every time. Trying all the solutions right now.

Check out the code in the client script. It may be unintentionally deleting the gui.

Well, I guess I might as well give you all the code so that I am not leaving anything out.

Local Script (client)

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent:TweenPosition(UDim2.new(2,0.1,0),"Out","Quad",1)

	local class = game:GetService("Players").LocalPlayer:WaitForChild("ClassFolder").ClassValue;
	
	if class.Value == 1
	then
	
		local Fire1 = game.ReplicatedStorage.class1
		Fire1:FireServer(script.Parent.Parent.Loadout.Value)
		print('sent remote')

	elseif class.Value == 2
	then
		local Fire2 = game.ReplicatedStorage.class2
		Fire2:FireServer(script.Parent.Parent.Loadout.Value)
	elseif class.Value == 3
	then
		local Fire3 = game.ReplicatedStorage.class3
		Fire3:FireServer(script.Parent.Parent.Loadout.Value)
	end
	
	script.Parent.Parent.Parent:Destroy()
	print("destroyed")
end)

Server Script

game.Players.PlayerAdded:Connect(function(plr)
		plr.CharacterAdded:Connect(function(char)
			local ClonedGUI = game.ServerStorage.ClassLoadout:Clone()
			ClonedGUI.Parent = plr.PlayerGui
			print("cloned")
	end)	
end)

Is this not working for every player when this error is reproduced by one player? or this is only reproduced for the player who did it?.

To test this I made a local server with 2 players. The GUI appeared on initial spawn, but did not appear on the following re-spawns. (on both)

Is there any error in the output?

none. I also receive my print that says “cloned” each time that the server script runs.

I don’t know, you could try to put more prints to debug in the Client code, or check in the Explorer for the gui.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local ClonedGUI = game.ServerStorage:WaitForChild("ClassLoadout"):Clone()
		ClonedGUI.Parent = plr.PlayerGui
		print("cloned")
	end)	
end)

If it doesn’t works, create an localscript in StarterPlayerScripts:

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() -- repeat wait() until plr.Character ~= nil

local ClonedGUI = game.ServerStorage:WaitForChild("ClassLoadout"):Clone()
ClonedGUI.Parent = plr.PlayerGui
print("Reached")

Well, I have tried all of these things, and several other, but I think I have concluded that my issue might be somewhere else. See, I removed the code that would delete the GUI when the button is pressed, and simply “disabled it,” yet, now whenever my player respawns, the GUI seems to be deleted from playerGUI. I am going to investigate a little and see if I can figure out what is causing this

Thank you all for your help. I found a solution.

Instead of keeping the GUI in server storage and cloning it into the users playerGUI each time they spawned, I simply “disabled” the GUI when the confirm button was clicked. Then, I had code that when the character is loaded, would wait a second (important), and enable the GUI once again.

2 Likes