For some reason the function does not replicate the GUI from ReplicatedStorage if I put Pet1 and Dog1 variables in front of it. Why does this happen? Doing :WaitForChild didn’t help by the way.
if (not game:IsLoaded()) then
game.Loaded:Wait()
end
local ReplicatedStorage = game.ReplicatedStorage
local Player = game.Players.LocalPlayer
local ReplicaScreenGui = ReplicatedStorage.ScreenGui
local Gui = Player.PlayerGui
local Pet1 = workspace.Pet1
local Dog1 = workspace.Dog1
Player.CharacterAdded:Connect(function()
ReplicaScreenGui:Clone().Parent = Gui
end)
local ScreenGui = Gui:WaitForChild("ScreenGui") -- It says here infinite yield possible if I put Pet1 and Dog1 variables before the function. This is because the ReplicaScreenGui didn't get cloned and parented for some reason.
you don’t have a variable named Client, it’s supposed to be Player
if (not game:IsLoaded()) then
game.Loaded:Wait()
end
local ReplicatedStorage = game.ReplicatedStorage
local Player = game.Players.LocalPlayer
local ReplicaScreenGui = ReplicatedStorage.ScreenGui
local Gui = Player.PlayerGui
local Pet1 = workspace.Pet1
local Dog1 = workspace.Dog1
Player.CharacterAdded:Connect(function()
ReplicaScreenGui:Clone().Parent = Gui
end)
local ScreenGui = Gui:WaitForChild("ScreenGui") -- It says here infinite yield possible if I put Pet1 and Dog1 variables before the function. This is because the ReplicaScreenGui didn't get cloned and parented for some reason.```
The variables got messed up when I was pasting them in the post. It is Player and not Client in the script and still doesn’t work. The thing is, that if I delete Pet1 and Dog1 variables the script does work. This is why I am curious why this is so.
Also, there is no need to provide the rest of the code since it does not affect the outcome. I have tested that by running exclusively this part of the code alone.
The paths to objects are also correct 100% since all of the code works properly if I place the variables in another place (Deleted was not the right word), even without :WaitForChild
Problem is that it can’t find ScreenGui just make a repeat until theres a screengui if theres no then just add one character loads faster then scripts sometimes
I know that the code stops working because it can’t find ScreenGui, I have stated that before. This is because for some reason when I put Pet1 and Dog1 variables before the function that replicates that exact ScreenGui, it stops working and doesn’t replicate. Now my question is why this is so.
Oh I see now. I didn’t get what you mean the first time I read your message.
I think I have found the answer to this, but I am not 100% sure. Basically, indexing an object in the PlayerService or ReplicatedStorage is acceptable, in this case, because their contents can already be accessed by the script, thus, the thread which the PlayerAdded event is on will not be delayed and it will be able to fire the function in case a player joins.
On the other hand, if we index an object from the Workspace, it will prevent the thread that the PlayerAdded event is on from firing if a player joins the game since objects in Workspace load slower, forcing us to use :WaitForChild (if we just index the object without :WaitForChild it will not work since you are indexing a yet non-existent thing).