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

Trying to clone a name tag from serverstorage. What am i doing wrong?

Name is a billboardGUI this script is in ServerScriptService output says : attempt to call method 'clone' nil value

local Nametag = game.ServerStorage.Name:clone()

game.Players.PlayerAdded:Connect(function(player)
	Nametag.TextLabel.text = player.Name
end)

1 comment

0

:clone is deprecated, use :Clone instead. You never know when deprecated stuff gets deleted.

4 answers

✓ Accepted answer

You see, there are many problems in your script.

##1. You use the void function :clone() instead of :Clone()

:clone() is not a function in out of any of the Roblox functions. The correct function is :Clone(). Roblox Lua is case-sensitive. This means that anything that is misspelled will be an error.

How to fix this line of code

--\\ Services
local serverStorage = game:GetService("ServerStorage")
--\\ Name Tag
local nameTag = serverStorage:WaitForChild("Name"):Clone()

I have also noticed in your script that you use .Name I am assuming you are accessing the NameTag. Avoid making models the names of properties.

##2. How you are cloning

Speaking of cloning, how you are cloning is just going to work for one player. To fix this, we need to clone the name tag to the player that is adding.

How to fix this

--\\ Services
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
--\\ Name Tag
local nameTag = serverStorage:WaitForChild("Name")
--\\ Add NameTag
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		workspace:WaitForChild(player.Name)
		local head = char:WaitForChild("Head")
		local newTag = nameTag:Clone()
		newTag.Parent = head
	end)
end)

Let's look at the revised code

We have added a CharacterAdded event to our PlayerAdded event. Since the model that we move, jump and etc. is the character. We need to wait for the character to load. We use CharacterAdded because the player loads faster than the character. If we were to access the character by doing local char = player.Character, it would be nil

##workspace:WaitForChild(player.Name)

This line of code is crucial. The method :WaitForChild() literally waits for the child. It takes the argument is a string. It also takes an optional argument which is the time out. This waits for the amount of seconds the time out is and then, it accesses the object.

You could use :WaitForChild() in variables or just put it as a line of code just to check if it is nil.

##The Difference between :WaitForChild() and Dot Notation Dot notation automatically checks the object For example:

local brick = workspace.Brick

This variable automatically checks the brick. We don't have a brick in workspace, so, the output would error. In contrary to :WaitForChild() :

local brick = workspace:WaitForChild("Brick")

This variable checks if there is the part named "Brick" in Workspace. Like the last example, there is no "Brick" in workspace. What makes :WaitForChild() different is that it waits a bit for the object to not become nil. It checks after the delay. If the object is nil, it does something called an infinite yield. An infinite yield doesn't come out as an error, but, it comes out as a warning

##3. Changing Text

Now, how can you change properties?

The answer is pretty simple. Let's take a look at our revision.

--\\ Services
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
--\\ Name Tag
local nameTag = serverStorage:WaitForChild("Name")
--\\ Add NameTag
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		workspace:WaitForChild(player.Name)
		local head = char:WaitForChild("Head")
		local newTag = nameTag:Clone()
		newTag.Parent = head
	end)
end)

You want to change the Text Label inside of the Name Tag to show the player name. We simply take the playerName variable and get the Text property. This property is a string. Our final edit to our code snippet:

--\\ Services
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
--\\ Name Tag
local nameTag = serverStorage:WaitForChild("Name")
--\\ Add NameTag
players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		workspace:WaitForChild(player.Name)
		local head = char:WaitForChild("Head")
		local newTag = nameTag:Clone()
		local playerName = newTag:WaitForChild("TextLabel")
		playerName.Text = player.Name
		newTag.Parent = head
	end)
end)

I should clarify a few things.

##Why did I change the text of the variable playerName?

It is good programming practice to change certain properties before parenting something. This tip could be put into our programming lives. For example, if we were to create a brick by using the class Instance.new(). You would usually put the second argument of Instance.new(). That is incorrect

local brick = Instance.new("Part")
brick.Name = "Brick"
brick.Color3 = Color3.fromRGB(0,0,0) -- 0,0,0 is black
brick.Parent = workspace

You see, I changed the brick's parent to workspace after changing the properties. This decreases lag and latency in your game.

##You wrote a property incorrectly

Like said before, Roblox Lua is case-sensitive. This is obviously the same case with changing properties.

An example of this:

local part = Instance.new("Part")
part.name = "this is incorrect" -- this is the incorrect way to access the Name property
part.Name = "this is correct" -- this is the correct way to access the Name property

##What is the hierarchy of this code? Now, where do you put the fixed code?

The Server Script's hierarchy:

game>ServerScriptService>Script Insert a script in ServerScriptService and copy-paste the final code

The Name Tag Hierarchy:

game>ServerStorage>Name I am assuming you have a billboard GUI in ServerStorage and a TextLabel in there

If you say that this script doesn't work, I have tested this out myself and it does work. Make sure you followed every step above.

Hopefully you have understood the mistakes in your code and hopefully you can apply them in other snippets of code you will write.

2 comments

0

You are really good at explaining, thx for the help!

0

Your welcome.

When you clone something, you need to always set a parent for it. The only thing you're doing is changing a property in it. So, it's always important to set its parent first then do all your changes. Also, don't do

local Nametag = game.ServerStorage.Name:Clone().Parent since it's not gonna work.

Right now, this will only work for 1 player because you only clone it once from the beginning of the game. For this to fully work not only you have to Parent it but you also have to clone it when a player joins.

local ServerStorage = game:GetService('ServerStorage')

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local Char = player.Character or player.CharacterAdded:Wait()
		local Name = ServerStorage:WaitForChild('Name'):Clone()
	    	Nametag.TextLabel.text = player.Name
		Name.Parent = Char:WaitForChild('Head')
	end)
end)

Hopefully this helps!

#EDITED: COPY AND PASTE THIS INTO A SERVER SCRIPT IN SERVER SCRIPT SERVICE#

2 comments

0

output still the same as above

0

What do you mean what is the output

You have to set the parent. Here's what I'd do:

local Nametag = game.ServerStorage.Name --Initializing Nametag

game.Players.PlayerAdded:Connect(function(player)
NameTag:Clone().Parent = player.PlayerGui --Where you want to put the nametag, could be somwhere else.
NameTag.TextLabel.Text = player.Name
end)

2 comments

0

the output is still the same

0

try cloning it to the workspace just to test

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