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

How can I get a player's name in a script of a model?

Basically, as a player joins, I want a model to be created in ReplicatedStorage and the model be named the name of the player. Then, I want an IntValue to be created inside of that Model, so I have this script, but studio is complaining that "model" is not a valid member of ReplicatedStorage. in line 7

game.Players.PlayerAdded:connect(function(player)	
	local entry = player.Name
	local model = Instance.new("Model", game.ReplicatedStorage)
	model.Name = entry
	local coins = Instance.new("IntValue")
	Coins.Parent = game.ReplicatedStorage.model
	coins.Name = "Coins"

2 answers

In line 3, you named model "Model"

Instance.new("Model", game.ReplicatedStorage)

Then in line 6, you referred to it as "model"

  Coins.Parent = game.ReplicatedStorage.model

I'm not sure if there are any other errors, but I do think you need to capitalize the first letter of model in line 6, and change "Coins" to "coins" at the start of line 6.

1 comment

0

Hope I helped. Please mark as answered if I did

iamnoamesa was going in the correct direction, it is because of the names.

So when you create an instance it has a default name:-

local  model = Instance.new("Model", game.ReplicatedStorage)
print('Default name is', model.Name)

-- change the name
model.Name = 'foo'
print('New name is', model.Name)

The second problem is that the replicated storage does not have a child called model, it would have "Model" if you used the default name:-

-- example one
local model = Instance.new('Model', game.ReplicatedStorage)
print('Model is called', game.ReplicatedStorage.Model)-- we add a model so now we use its name to access it

-- example two
local model2 = Instance.new('Model', game.ReplicatedStorage)
model2.Name = 'foo'
print('Model is called', game.ReplicatedStorage.foo) -- we have changed its name so we need to access it using the new name

Lastly as we have the variable which points to the model object we can just use that as its parent we do not require its full path:-

game.Players.PlayerAdded:connect(function(player)   
	-- we do not need this unless we access it multiple times
    --local entry = player.Name
    local model = Instance.new("Model", game.ReplicatedStorage)
    model.Name = player.Name
	-- this will set the parent of the intvalue to the model we just created using its variable
    local coins = Instance.new("IntValue", model) -- we can use the second optional arg 
	-- Lua is case-sensitive so 'Coins'  is not the same as 'coins'
    --Coins.Parent = game.ReplicatedStorage.model
    coins.Name = "Coins"
end)

I hope this helps, please comment if you do not understand how / why this code works.

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