How do I get screenGui (PlayerGui) in a module script?

I’m using Gnome Codes Tower Defense Tutorial, and unlike his. I want my health to always be on the top rather than hovering at the exit.

I made the GUI just fine but when I try to find it, I have to use localplayer.playergui, which than I get the error of “attempt to index nil with ‘PlayerGui’” I don’t know how to fix this and how to find playergui through a module script but I hope someone can help!

All I’ve tried was WaitForChild(PlayerGui) and just a plain .playergui. I also couldn’t find anything on here or the developer hub.

local baseHealth = {}

function baseHealth.Setup(map, health)
	baseHealth.HurtHealth = health
	baseHealth.Health = health
end

function baseHealth.updateHealth(damage)
	if damage then
		baseHealth.Health -= damage
	end
	
	local gui = game.Players.LocalPlayer.PlayerGui.mainScreen
	local percent = baseHealth.Health / baseHealth.HurtHealth
	
	gui.Health.Size = UDim2.new(percent, 0, 1, 0)
end

return baseHealth

This is the base script for it, it error’s at playergui and idk what to do! I hope someone can help.

should be WaitForChild(“PlayerGui”)

an issue also can be if you are trying to call this function in a server script, which doesnt have access to localplayer

Alright that’s the problem, is there any works around it? Do I call it to a local script somehow?
image
Even when it located inside the serverscript?
There would actually be one problem though, if a person where to take a year to load in, when he loads in. If they took damage he would still have max because he wasn’t loaded when the localscript fired the health.

game.Players.LocalPlayer works only in local scripts.

This doesnt mean that you have to do it in a local script, just get a reference to your player another way

Where is this main script located, screenshot me your entire hierarchy

ServerScriptService, I don’t really know what to do if it’s in literally SERVERscriptservice but there’s the info you wanted.

I’d need to get the player in a way where I can get playergui, also I have to go to school now. So if I don’t replay in awhile that’s why!

Great, good luck with that mate)

Make sure the script is local, and located in a replicated parent.

Also, you have to close ‘PlayerGui’ in quotations like this:

WaitForChild("PlayerGui")

Remember that only code in a localscript can see this instance, as it is not replicated onto the server!!

Actually, it is replicated onto the server.

its not possible
what you can do is

-- script
local call = require(module)
call.addplrui(player)

-- modulescript
local yea = {}

local screenUI

function yea.addplrui(plrui)
	screenUI = plrui
end

return yea

I should have been more specific.

The reference to PlayerGui is (LocalPlayer.PlayerGui) is not replicated onto the server. Oops… :sweat_smile:

The reference to LocalPlayer is not replicated onto the server. :slightly_smiling_face:

According to my tests (by adding a ScreenGui with a Frame in StarterGui and changing the size of the frame and checking on the server), it does not replicate to the server.

Amd, you should be doing UI stuff on the client not the server.

You could just create a LocalScript and update the GUI whenever the health changes.

I was talking about this. He said “only code in a local script can see this instance”, which is not true.

Depends on the context in which the ModuleScript is required, if the ModuleScript is required by a local script then you can do the following.

--LOCAL

local players = game:GetService("Players")
local player = players.LocalPlayer
local module = script:WaitForChild("ModuleScript")
module = require(module)
local playerGui = module.getPlayerGui(player)
print(playerGui.Name)
--MODULE

local module = {}

function module.getPlayerGui(player)
	local playerGui = player:WaitForChild("PlayerGui")
	return playerGui
end

return module

If the ModuleScript is required by a server script then you can do the following.

--SERVER

local players = game:GetService("Players")
local module = script.ModuleScript
module = require(module)

players.PlayerAdded:Connect(function(player)
	local playerGui = module.getPlayerGui(player)
	print(playerGui.Name)
end)
--MODULE

local module = {}

function module.getPlayerGui(player)
	local playerGui = player:WaitForChild("PlayerGui")
	return playerGui
end

return module

Try not to overcomplicate things, notice how the ModuleScript is the same regardless of the context in which it is required.

1 Like