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
local serverStorage = game:GetService("ServerStorage")
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
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local nameTag = serverStorage:WaitForChild("Name")
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.
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local nameTag = serverStorage:WaitForChild("Name")
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:
local serverStorage = game:GetService("ServerStorage")
local players = game:GetService("Players")
local nameTag = serverStorage:WaitForChild("Name")
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)
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"
part.Name = "this is correct"
##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.
1 comment
:clone is deprecated, use :Clone instead. You never know when deprecated stuff gets deleted.