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

WaitForChild from a localscript?

Hello, I have the following script that is designed to load (or supposed to, lol) uniforms when a certain button is clicked. For reference, UniformLoader is a LocalScript located inside StarterGui, which serves the purpose of creating the UI. It also houses three BindableEvents, LoadFrench, LoadBritish, and LoadPrussian. The 'print("Receiver fired")' is only there for debugging purposes. This script itself is a Script located inside StarterCharacterScripts.

However, upon testing I get the following error. "Infinite yield possible on 'Players.cooldeath49.PlayerGui:WaitForChild("UniformLoader"). Does anyone know why?


local Character = script.Parent
local Player = game.Players:GetPlayerFromCharacter(Character)
local UniformLoader = Player.PlayerGui:WaitForChild("UniformLoader")
local Shirt = Player.Shirt.ShirtTemplate
local Pants = Player.Pants.PantsTemplate
local FrenchFired = UniformLoader:WaitForChild("LoadFrench")
local BritishFired = UniformLoader:WaitForChild("LoadBritish")
local PrussianFired = UniformLoader:WaitForChild("LoadPrussian")


function LoadFrench()
	Shirt = "rbxassetid://1163144329"
	Pants = "rbxassetid://1587606477"
	print("Receiver fired")
end

function LoadBritish()
	Shirt = "rbxassetid://1979891956"
	Pants = "rbxassetid://1979911648"
	print("Receiver fired")
end

function LoadPrussian()
	Shirt = "rbxassetid://176660910"
	Pants = "rbxassetid://176660953"
	print("Receiver fired")
end

BritishFired.Event:Connect(LoadBritish)
PrussianFired.Event:Connect(LoadPrussian)
FrenchFired.Event:Connect(LoadFrench)

A response would be much appreciated! Thank you!

2 comments

0

Are you sure you have a child "UniformLoader" in the PlayerGui?

0

That's a warning not an error

2 answers

local UniformLoader = Player.PlayerGui:WaitForChild("UniformLoader")

PlayerGui is not replicated from the Client to the Server, as a result, neither is the UniformLoader LocalScript, at least not in principle. Starter - ScreenGuis and LocalScripts will show up under the player, but they are unable to be read from or changed on the Server.

This being said, you may want to switch to a Remote Event instead for this since you will need to communicate between the server and client.

Server Script Example

local Character = script.Parent
local Shirt = Character:WaitForChild("Shirt")
local Pants = Character:WaitForChild("Pants")

local Remote = Instance.new("RemoteEvent")
Remote.Name = "OutfitChange"
Remote.Parent = game:GetService("ReplicatedStorage")

Remote.OnServerEvent:Connect(function(player, region)
	if region == "French" then
		Shirt.ShirtTemplate = "rbxassetid://1163144329"
		Pants.PantsTemplate = "rbxassetid://1587606477"
	elseif region == "British" then
		Shirt.ShirtTemplate = "rbxassetid://1979891956"
		Pants.PantsTemplate = "rbxassetid://1979911648"
	elseif region == "Prussian" then
		Shirt.ShirtTemplate = "rbxassetid://176660910"
		Pants.PantsTemplate = "rbxassetid://176660953"
	end
	print("Receiver fired")
end)

LocalScript Counterpart Example

local Player = game.Players.LocalPlayer
local Remote = game.ReplicatedStorage:WaitForChild("OutfitChange")

repeat wait() until Player.Character ~= nil and workspace:FindFirstChild(Player.Name) ~= nil

local random = math.random(1, 3)
if random == 1 then
	Remote:FireServer("French")
elseif random == 2 then
	Remote:FireServer("British")
elseif random == 3 then
	Remote:FireServer("Prussian")
end

This happens a lot with the PlayerGui, as Incapaxx said this isn't an error, it's a warning. Basically I believe it's happening because everything within the starterGui gets cloned over to every player gui upon them joining, so sometimes there is a bit of lag which causes that. At least that has been the issue for me.. (I could be wrong, but that seems to be an issue that I too have came across myself)

There are a few ways around this, What I do is just manually insert the gui within a local script and work from there

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