Why is :WaitForChild not working?

Hey developers,

I have gamepass script in Serverscriptservice that basically just parents a button to a gui, the gui does is parented to a Replicated storage folder.

Another script will parent the gui from the Replicated storage folder to the Playergui but, the problem is that when the gui is parented to Playergui the button won’t be parented to the gui.

Here is the script (in serverscriptservice)

local mps = game:GetService("MarketplaceService")
local gamepassid = 21391533


game.Players.PlayerAdded:Connect(function(player)
	if mps:UserOwnsGamePassAsync(player.UserId, gamepassid) then
		script.shop:Clone().Parent = player.PlayerGui:WaitForChild("MainGui").Background
		
		script.exclusive:Clone().Parent = player.PlayerGui:WaitForChild("MainGui").Background.Shop.Frame
	else
		script.button:Clone().Parent = player.PlayerGui:WaitForChild("MainGui").Background.Shop.Frame
		
	end
end)


game.ReplicatedStorage.GuiPurchasedEXCLUSIVE.OnServerEvent:Connect(function(player)
	script.shop:Clone().Parent = player.PlayerGui:WaitForChild("MainGui").Background
	script.exclusive:Clone().Parent = player.PlayerGui:WaitForChild("MainGui").Background.Shop.Frame
end)

Help is appreciated!

1 Like

I think WaitForChild/FindFirstChild dont work on ServerScriptService

1 Like

Are there any errors in the output? You said that the GUI are in ReplicatedStorage, but in the script, you are mentioning that the GUI’s are inside the script?
Which part of the script does not work?
Is it this

or this?

Also, i dont think WaitForChild is the problem at all.

1 Like

Hey @BabyNinjaTime, I thought “WaitForChild” was the issue because with “WaitForChild” the script should be waiting on the MainGui.

image
This is the the output error, as you can see the script tried to detect the MainGui but it did not detect it at all because the script ran before the gui could be parented to PlayerGui.

1 Like

Is this true? I am not sure if they do.

Okay. Did you check to see if the MainGui is even in the PlayerGui? Could you show the script that parents the MainGui to PlayerGui?

1 Like

No that is not true. WaitForChild() (and FindFirstChild()) can be used in local scripts and server scripts (and module scripts as well), no matter where the scripts are located at (well local scripts should not be in ServerScriptService and other places but yea).

1 Like
local plr = game.Players.LocalPlayer
local inputService = game:GetService("UserInputService")



if inputService.TouchEnabled and not inputService.KeyboardEnabled then


	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("Mobile"):WaitForChild("MainGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("Mobile"):WaitForChild("SafeZoneGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("Mobile"):WaitForChild("CameraGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("Mobile"):WaitForChild("AvatarEditor"):clone().Parent = plr:WaitForChild("PlayerGui")


elseif inputService.GamepadEnabled and not inputService.KeyboardEnabled then


	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("Console"):WaitForChild("Guithing"):clone().Parent = plr:WaitForChild("PlayerGui")


else


	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("PC"):WaitForChild("MainGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("PC"):WaitForChild("SafeZoneGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("PC"):WaitForChild("CameraGui"):clone().Parent = plr:WaitForChild("PlayerGui")
	game:WaitForChild("ReplicatedStorage"):WaitForChild("Platforms"):WaitForChild("PC"):WaitForChild("AvatarEditor"):clone().Parent = plr:WaitForChild("PlayerGui")
end


wait(10)
script:destroy()

Yes, when I was trying to figure out why it would not work I checked from the server and the MainGui was part of my PlayerGui but the script never detected it (My guess is it tried to detect it to early)

If the case is that it detected it to early how can I fix it?

Okay, I found the problem. You are trying to clone the MainGui to PlayerGui on a LocalScript. Meaning that you are doing the stuff on the Client Side. The Server cannot detect Client changes, which is why the Server Script cannot find the MainGui in PlayerGui.

3 Likes

@BabyNinjaTime, Got it how can I fix this???

2 Likes

Help is still very much appreciated.

1 Like

Hey man, sorry for replying so late, I actually got disconnected, and I had to go somewhere. But yea i am back from where I was and now I can help.
I will be replying again below once im finished.

Okay, so, you want to use remote events in this situation.

Insert a remote event in ReplicatedStorage, and rename it to GuiEvent (you can change the name if you want, but just make sure to change the name in the scripts too).

Then use this code for your LocalScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

local Event = ReplicatedStorage:WaitForChild("GuiEvent")

if UserInputService.TouchedEnabled and not UserInputService.KeyboardEnabled then
	Event:FireServer("onMobile")

elseif UserInputService.GamepadEnabled and not UserInputService.KeyboardEnabled then
	Event:FireServer("onConsole")
	
else
	Event:FireServer("onPC")
end

Now, insert a Server Script in ServerScriptService, and use the following code below:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Platforms = ReplicatedStorage:WaitForChild("Platforms")
local Event = ReplicatedStorage:WaitForChild("GuiEvent")

local TypePC = Platforms.PC
local TypeMobile = Platforms.Mobile
local TypeConsole = Platforms.Console

Event.OnServerEvent:Connect(function(player, PlatformName)

	if PlatformName == "onMobile" then
		local Clone1 = TypeMobile:WaitForChild("MainGui"):Clone()
		local Clone2 = TypeMobile:WaitForChild("SafeZoneGui"):Clone()
		local Clone3 = TypeMobile:WaitForChild("CameraGui"):Clone()
		local Clone4 = TypeMobile:WaitForChild("AvatarEditor"):Clone()
		
		Clone1.Parent = player.PlayerGui
		Clone2.Parent = player.PlayerGui
		Clone3.Parent = player.PlayerGui
		Clone4.Parent = player.PlayerGui
	
	elseif PlatformName == "onConsole" then
		local CloneGui = TypeConsole:WaitForChild("Guithing"):Clone()
		CloneGui.Parent = player.PlayerGui
		
	elseif PlatformName == "onPC" then
		local Clone1 = TypePC:WaitForChild("MainGui"):Clone()
		local Clone2 = TypePC:WaitForChild("SafeZoneGui"):Clone()
		local Clone3 = TypePC:WaitForChild("CameraGui"):Clone()
		local Clone4 = TypePC:WaitForChild("AvatarEditor"):Clone()
		
		Clone1.Parent = player.PlayerGui
		Clone2.Parent = player.PlayerGui
		Clone3.Parent = player.PlayerGui
		Clone4.Parent = player.PlayerGui
	end
end)

And you are done.

1 Like

Thank you so much, I really do appreciate it!! (sorry for the late reply)

1 Like

You are very welcome, happy to help!

1 Like