Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Variable in Tree?

Asked by 8 years ago

Having a small problem, probably with an easy fix. How do I find PlrName in workspace in order to edit pants. plrname is not a valid member of Workspace

game.Players.PlayerAdded:connect(function(player)
    local plrname = player.Name
    workspace.plrname.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=18103438"
end)

Thanks :) Sorry if it's ridiculously easy. lol

2 answers

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

When you say something like game.Workspace, that is sugar for game["Workspace"].

In other words, . is just table-access. And you could, instead of writing game.Workspace write something like

local n = "Workspace"

local w = game[n]

In this case, you could use workspace[player.Name] to find the thing in the workspace with the same name as player.

However, you'll get an error if the object doesn't exist. If you aren't sure that it will exist, you should be using workspace:FindFirstChild(player.Name).

However -- this is NOT how you should be doing this. Player objects have a .Character property which gives you their Character model.

They also have a .CharacterAdded event which will give you their character each time they respawn. Use those.


You normally never need to inspect ROBLOX objects based on their name. You almost always should just keep the thing itself and use its properties, instead of getting a piece of the information and then doing your own search.

Ad
Log in to vote
0
Answered by 8 years ago

You can't search Workspace for a string. You have to search for the name of an object. player.Name is a string, so it won't work. There's a much easier way to get to the Character anyway. Just do this:

game.Players.PlayerAdded:connect(function(player)
    player.Character.Shirt.ShirtTemplate = "http://www.roblox.com/asset/?id=18103438"
end)

I just said player.Character because there's a character value inside the actual player object that leads to the player's character

Nice shirt, btw :P

0
Thanks! Lol Increated 25 — 8y
0
Nevermind qq Workspace.Script:2: attempt to index field 'Character' (a nil value) Increated 25 — 8y
0
Add the CharacterAdded event of player objects and run a function through that. M39a9am3R 3210 — 8y
0
I can't believe I forgot to mention the CharacterAdded event... This is quite embarrassing, but I see your question is answered, so no worries lightpower26 399 — 8y

Answer this question