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

"Position is not a valid member of Workspace?"

Asked by 6 years ago

Hey! I'm fairly new to ROBLOX Lua and this website too. Furthermore, I recently created a script that would change the position of a newly created object called "part". There is no actual object called "part" in my workspace. What I'm trying to do is create a part and change its location. So I came up with this...

part = Instance.new("Part", game.Workspace)
part = script.Parent
part.Position = Vector3.new(27, 0.5, -3)

However, when I run the script, the output says: 07:04:33.066 - Position is not a valid member of Workspace 07:04:33.067 - Stack Begin 07:04:33.068 - Script 'Workspace.Script', Line 3 07:04:33.069 - Stack End

Please Help...

1 answer

Log in to vote
1
Answered by 6 years ago

First you should be using local variables where possible.

local part = Instance.new("Part", game.Workspace)

Secondly the Parent argument for Instance.new should not be used. This is because of network replication as when the instance is part of the game the changes made to this instance would be replicated. Setup the instance then parent it to avoid needless network replication of changes.

Lastly your error is caused by you assigning part to hold a reference to the scripts parent which in this case is the workspace. I think you have mixed up how to set the parent and using the parent argument for Instance.new.

part = script.Parent -- this sets the variable part to the scripts parent causing your error

Putting this all together.

local part = Instance.new("Part") -- create instance no parent arg meaning the parent is nil

-- setup the part
part.Position = Vector3.new(27, 0.5, -3)

-- parent the part to the game
part.Parent = workspace

Hope this helps.

0
What's the difference between "local variables" and regular variables. In what instances are they used in, and what difference does it make? Thanks! T0BIUCHIHAA 11 — 6y
0
a local variable can only be accessed inside its scope not outside of it. a normal one (global) can be accessed anywhere in the script User#23365 30 — 6y
Ad

Answer this question