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

Why does this give me an error?

Asked by
Wubblu 20
9 years ago

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

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

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.

Ad

Answer this question