Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Simple script to print a ScreenGui in a player's PlayerGui on connect not working, why?

Asked by 7 years ago
Edited 7 years ago

I'm doing a test script to play around with for loops & looping through available players. The script is simple!

print("Script initiated.")
function Run()
    for i, v in ipairs(game.Players:GetPlayers()) do -- Get available players!
        wait(12); -- In case contents inside PlayerGui has not been loaded!
        print(v); -- Prints the current iteration, player's name (current iteration).
        print(game.Players[v.Name].PlayerGui.testFind); -- Simple, there's a ScreenGui!
    end
end

game.Players.PlayerAdded:connect(Run)

Very simple. Yet doesn't work. Why!?

It works in studio and this is what it outputs:

Script initiated.
Player1
testFind

However if you try it online, there's suddenly an error!

Script initiated.
Player... added
Arithmeticity
testFind is not a valid member of PlayerGui

The script is placed in ServerScriptStorage.

Here's the model if you want to take a look at it and see why: click to see the model.

Am I missing something? Please help me as this is confusing me. It should be e-z-p-z.

Update(s):

Just got helped by ScriptGuider. He says that server cannot access the contents of PlayerGui so that may be a possibility. I'm now trying to work around it instead of doing this method. See how it goes and all, so far so good. Thanks to ScriptGuider! If ScriptGuider reads this, maybe post a reply so I can accept your answer perhaps.

Edited the script yet again to include WaitForChild. Here it is:

print("Script initiated.")
function Run()
    for i, v in ipairs(game.Players:GetPlayers()) do 
        print(v);
        game.Players[v.Name].PlayerGui:WaitForChild("testFind");
        print(game.Players[v.Name].PlayerGui.testFind);
    end
end

game.Players.PlayerAdded:connect(Run)

Output has been changed, it's now this:

Script initiated.
Arithmeticity.
Player ... added
Infinite yield possible on 'Players.Arithmeticity.PlayerGui:WaitForChild("testFind")'
Stack Begin
Script 'ServerScriptService.testIterations', Line 5
Stack End

I have also updated the model so if anyone can please take it and take a look in their own game, that'll be really helpful!

Past edit 2/1/2017:

print("Script initiated.")
function Run()
    for i, v in ipairs(game.Players:GetPlayers()) do 
        print(v);
        repeat wait() until game.Players[v.Name].PlayerGui.testFind;
        print(game.Players[v.Name].PlayerGui.testFind);
    end
end

game.Players.PlayerAdded:connect(Run)

The output is pretty much the same results. Why is this happening!?

0
Are you trying to do something to every player every time a player joins, or just do something to the player that just joined? GoldenPhysics 474 — 7y
0
Commented Shawnyg 4330 — 7y
0
I'm not sure but i think you should try making it a local script and putting it into game.StarterPlayer.StarterPlayerScripts Nogalo 148 — 7y
0
@Nogalo, that defeats the purpose of what I'm trying to do. Do you not have a solution? Arithmeticity 167 — 7y
View all comments (2 more)
0
Why're you typing "game.Players[v.Name]" when "v" is the player returned? TheeDeathCaster 2368 — 7y
0
Is the GUI even being put into the PlayerGui? If it's a script placing it there, please show it. Shawnyg 4330 — 7y

3 answers

Log in to vote
0
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
7 years ago

When dealing with the local player, you're going to need WaitForChild. The method will not permit the script to continue until the specified object is found, which in your case, is a GUI. I recommend you get familiar with the method, if you're going to be dealing with he client a lot.

0
However, the thing is that I've already used wait. Localplayer was already found by the result of its print(v) therefore shouldn't this not be an issu? Repeat wait() is the same thing as well, no? Arithmeticity 167 — 7y
0
Sure the Player was found, however its descendants haven't fully loaded. Using a wait() for the GUI isn't logical since you'd have to specify the exact location, even though it isn't there yet. Using WaitForChild doesn't pinpoint it using "." Shawnyg 4330 — 7y
0
@Shawnyg: I edited my post to show that I've tried your solution. It does not work. It introduced a new error. I don't know why. Is it possible for you to take my model and take a look at it, maybe? What did I do wrong??? Arithmeticity 167 — 7y
Ad
Log in to vote
0
Answered by
oGxbe 0
7 years ago

WaitForChild is needed.

Log in to vote
-1
Answered by 7 years ago
Edited 7 years ago

Instead of using wait, try using a repeat until system. Like so:

repeat wait() until game.Players[v.Name].PlayerGui.testFind

That could solve your issue, also, semicolons are not necessary in Lua code... just to let you know.

0
That will keep waiting until testFind is a child of PlayerGui xXLegendarySoldierXx 129 — 7y
0
I tried, it yields the same results. Why is that? I use semi colons b/c. sometimes I use other languages that does use them. Arithmeticity 167 — 7y
0
Like Java or JavaScript, I understand. Semicolons won't hurt the code so its fine. xXLegendarySoldierXx 129 — 7y
0
Your script has problems: if PlayerGui doesn't exist, it'll break, and it's not checking for if 'testFind' exists: you need to use "FindFirstChild" in this case. TheeDeathCaster 2368 — 7y
0
I've gotten some help by ScriptGuider. ScriptGuider says that server cannot access contents of PlayerGui. If this is true, this is something new to me. It's terrible too. Can anyone confirm? Arithmeticity 167 — 7y

Answer this question