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

Why does this give me an error?

So my code is:


playerName = script.Parent.Parent.Parent
game.Workspace.playerName.remove()

But it says "playerName is not a valid member of Workspace" Why?

1 answer

There are a bunch of things wrong with this.

Methods

Methods are functions that act on objects. You use : to call them, instead of ..

Also, in ROBLOX all methods are Uppercase (though for many lowercase is valid it's deprecated). You're also recommended to use :Destroy() over :Remove().

Indexing

When you say workspace.playerName, you are asking for the thing name "playerName" -- it does not care what the variable playerName is, it's asking literally for something with the name "playerName". There is no such thing, and the error is informing you of this.

If you want to get based on a variable, you use obj[variable] instead of obj.name, e.g., game["Workspace"] is the same thing as game.Workspace.

Thus it would be workspace[playerName]:Destroy()

Models

However, playerName isn't a name at all -- it's a model. If you wanted to get the .Name of script.Parent.Parent.Parent, you should do that explicitly.

But there's no point in doing that, since you can just :Destroy() the player directly:

player = script.Parent.Parent.Parent
player:Destroy()

It's a bad idea to "look up" the name when you could just hold onto the object itself.

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