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

"Position is not a valid member of Workspace?"

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

✓ Accepted answer

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.

4 comments

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!

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

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