#PlayerNameUIModule

1 messages · Page 1 of 1 (latest)

fringe badge
#

I have The Problem with the following. i get this error and i cant fix it "ReplicatedStorage.PlayerNameUIModule:4: attempt to index nil with 'Character' - Server - PlayerNameUIModule:4"

local PlayerNameUIModule = {}

function PlayerNameUIModule:init(player)
   local Character = player.Character <----- **Error here**
   local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

   local BillboardGui = Instance.new("BillboardGui")
   BillboardGui.Adornee = HumanoidRootPart
   BillboardGui.Parent = Character
   BillboardGui.Size = UDim2.new(0, 100, 0, 25)
   BillboardGui.StudsOffset = Vector3.new(0, 3, 0)
   BillboardGui.MaxDistance = 30
   BillboardGui.AlwaysOnTop = true

   BillboardGui.Name = "PlayerNameBillboard"

   local TextLabel = Instance.new("TextLabel")
   TextLabel.SizeConstraint = Enum.SizeConstraint.RelativeXY
   TextLabel.Parent = BillboardGui
   TextLabel.Size = UDim2.new(1, 0, 1, 0)
   TextLabel.BackgroundTransparency = 1
   TextLabel.TextColor3 = Color3.new(1, 1, 1)
   TextLabel.TextScaled = true
   TextLabel.Font = Enum.Font.SourceSansBold
   TextLabel.Text = player.Name
end

return PlayerNameUIModule

and i got this LocalScript in StarterCharacterScript

-- LocalScript
local PlayerNameUIModule = require(game.ReplicatedStorage:WaitForChild("PlayerNameUIModule"))
local LocalPlayer = game.Players.LocalPlayer

local function onCharacterAdded(character)
   PlayerNameUIModule:init(LocalPlayer)
end

LocalPlayer.CharacterAdded:Connect(onCharacterAdded)

i can refrence the Character in the localscript but not in the module in ReplicatedStorage

hard galleon
#

Seems like the localplayer isint going trough

#

So this is a server and module script?

fringe badge
hard galleon
#

Try to replace the module script second line: playernameUI:init() to playerbameui.init()

#

Since the : adds a self argument

fringe badge
#

nah didnt help

hard galleon
#

Hm

#

Okay so 2 things

#

1 its easier to just premake a billboardgui and put in the script and copy that

sleek cipher
#

why do you do game.players.Localplayer

#

when its in startercharacter you can just do script.parent

#

but when I test it theres no error

#

so its bc of your game

#

it takes a while to load the character

hard galleon
#

2 when you rewuire it the player is not defined yet

fringe badge
# sleek cipher when its in startercharacter you can just do script.parent

so you copied my code 1/1 and it worked ?
`
-- LocalScript

local PlayerNameUIModule = require(game.ReplicatedStorage:WaitForChild("PlayerNameUIModule"))
local LocalPlayer = script.parent

local function onCharacterAdded(character)
PlayerNameUIModule.init(LocalPlayer)
end

LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
`

`local PlayerNameUIModule = {}

function PlayerNameUIModule.init(player)
local Character = player.Character
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

local BillboardGui = Instance.new("BillboardGui")
BillboardGui.Adornee = HumanoidRootPart
BillboardGui.Parent = Character
BillboardGui.Size = UDim2.new(0, 100, 0, 25)
BillboardGui.StudsOffset = Vector3.new(0, 3, 0)
BillboardGui.MaxDistance = 30
BillboardGui.AlwaysOnTop = true

BillboardGui.Name = "PlayerNameBillboard"

local TextLabel = Instance.new("TextLabel")
TextLabel.SizeConstraint = Enum.SizeConstraint.RelativeXY
TextLabel.Parent = BillboardGui
TextLabel.Size = UDim2.new(1, 0, 1, 0)
TextLabel.BackgroundTransparency = 1
TextLabel.TextColor3 = Color3.new(1, 1, 1)
TextLabel.TextScaled = true
TextLabel.Font = Enum.Font.SourceSansBold
TextLabel.Text = player.Name

end

return PlayerNameUIModule
`

sleek cipher
#

I did copy it

#

but nothing happens

#

but no error

#

Im checking

#

function PlayerNameUIModule:Init(player)
    
    local Character = player.Character 
        local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")

    local BillboardGui = Instance.new("BillboardGui")
    BillboardGui.Adornee = HumanoidRootPart
    BillboardGui.Parent = Character
    BillboardGui.Size = UDim2.new(0, 100, 0, 25)
    BillboardGui.StudsOffset = Vector3.new(0, 3, 0)
    BillboardGui.MaxDistance = 30
    BillboardGui.AlwaysOnTop = true

    BillboardGui.Name = "PlayerNameBillboard"

    local TextLabel = Instance.new("TextLabel")
    TextLabel.SizeConstraint = Enum.SizeConstraint.RelativeXY
    TextLabel.Parent = BillboardGui
    TextLabel.Size = UDim2.new(1, 0, 1, 0)
    TextLabel.BackgroundTransparency = 1
    TextLabel.TextColor3 = Color3.new(1, 1, 1)
    TextLabel.TextScaled = true
    TextLabel.Font = Enum.Font.SourceSansBold
    TextLabel.Text = player.Name
end

return PlayerNameUIModule```
#

local PlayerService = game:GetService("Players")
local PlayerNameUIModule = require(game.ReplicatedStorage:WaitForChild("PlayerNameUIModule"))
local char = script.Parent
local player = PlayerService:GetPlayerFromCharacter(char)


local function onCharacterAdded(character)
    
    PlayerNameUIModule:Init(player)
    
end


if char then
    
    onCharacterAdded(char)
    
end```
#

done

hard galleon
fringe badge
#

so player service?

hard galleon
#

And make it in startercharacter

sleek cipher
#

no the module is ok

#

if he wanna practice

#

not really needed

fringe badge
hard galleon
#

True

sleek cipher
#

but its better to read

hard galleon
#

Fsir

sleek cipher
#

you get the player

#

with playerservice

#

you do ```
local player = PlayerService:GetPlayerFromCharacter(char)````

#

sometimes characteradded doesnt work

#

so u just do if char then

#

its the same

hard galleon
sleek cipher
#

yea

#

thats why script.parent

fringe badge
sleek cipher
#

and not local bla bla

sleek cipher
#

but

#

idk how to explain

fringe badge
hard galleon
sleek cipher
#

server

#

no nametag

#

you have to do it with remote

hard galleon
#

Remotevents are really good

fringe badge
#

true.

i basicly just use module.

1 localScript
1 ServerScript in serverservice
and the rest is just remotes and module

sleek cipher
#

Modules are really amazing

fringe badge
#

looks better i thing

sleek cipher
#

but theyre also needed for bigger projects

#

also because of memory leak

hard galleon
#

No

#

All you need is 1 serverscript and if you want 1 module

sleek cipher
#

for the nametag?

hard galleon
#

Yeah

#

But it StarterCharacter

fringe badge
olive needleBOT
#

studio** You are now Level 7! **studio

fringe badge
#

i dont even need a localscript