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

how do I fix the problem,"attempt to index nil with 'PlayerGui'"?

I used the code,

game.Players.LocalPlayer.PlayerGui.ScreenGui.Bcrops.Text

in the script but an error shows up telling me that localplayer is nil

Workspace.Test.Script:25: attempt to index nil with 'WaitForChild'

so , i put :WaitForChild on my line like this ,

game.Players:WaitForChild("LocalPlayer").PlayerGui.ScreenGui.Bcrops.Text

then a yellow text shows up saying there are infinite yield that is possible on 'Players:WaitForChild("LocalPlayer")'

 Infinite yield possible on 'Players:WaitForChild("LocalPlayer")'

how do I fix it plz help me

2 comments

0

LocalPlayer ONLY works for local scripts

0

But can a localscript connect to modulescript ?

2 answers

That's because you have to specify which person has entered the game in this case its a localplayer. LocalPlayers can only be accessed in a localscript and you cannot Wait For a child which is not there. Because the player might not be in the game. If you want to learn more about localplayers visit:

your code should be something like this: https://developer.roblox.com/en-us/api-reference/property/Players/localPlayer/index.html

5 comments

0

Can you do require(script.ModuleScript) on a localscript ?

0

Yes, you can use require() in local scripts but I don't think that's what you really need.

0

Of course.

0

Please accept my answer.

LocalPlayer cannot be used in scripts. You don't provide any detail about what you're trying to do so I am unsure which way would be optimal for you to use, but there are several ways to get a player.

PlayerAdded event - An event of the Players service which fires whenever a player joins the game. Returns the player that joined.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	print(player.Name .. " joined the game.")
end)

MouseClick event - An event of click detectors which fires whenever the part the click detector is inside of is clicked. Returns the player that clicked.

local ClickDetector = workspace.Part.ClickDetector

ClickDetector.Mouseclick:Connect(function(player)
	print(player.Name .. " clicked the part."
end)

These are just two examples. If you provide what you are trying to accomplish, we can suggest the most optimal way to retrieve a player.

2 comments

0

You got that from the roblox developer hub.

0

Incorrect. I typed those two examples myself. If you don't believe me, I made some typos so I had to edit them. But what are you trying to say? People copy and past examples from there all the time. It's just coincidence that I did the same example.

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