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.
Alright that’s the problem, is there any works around it? Do I call it to a local script somehow?
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.
-- script
local call = require(module)
call.addplrui(player)
-- modulescript
local yea = {}
local screenUI
function yea.addplrui(plrui)
screenUI = plrui
end
return yea
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.
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.