Why the script stops working when I create a variable with a path to an object in workspace?

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.
3 Likes

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.```
2 Likes

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.

1 Like

It is because you didn’t wait for them to load. Have you tried using WaitForChild on them yet?

1 Like

You can attempt using:

local Pet1 = game.Workspace:FindFirstChild('Pet1')
local Dog1 = game.Workspace:FindFirstChild('Dog1')

Although the variables aren’t being used anyway, so I suggest you remove them from the script entirely.

2 Likes

I think he uses them later on, the code he provided is probably a snippet.

1 Like

I suggest that they’d provide us with the full code as I don’t see any issue with the current whatsoever.

1 Like

Idk I think it’s just loading slow and he didn’t add a WaitForChild on those items.

1 Like

As I have said in the post:

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

2 Likes

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

1 Like

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.

1 Like

As I said earlier its because character loads faster then your script.

2 Likes

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).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.