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

Why does this script respond with a bad output instead of creating an instance?

Player = game:GetService('Players').LocalPlayer
mouse = Player:GetMouse()

function onKeyDown(key)
	key = key:lower()
	if key == "z" then
		print("Success")
		Instance.new("Part")
		game.Workspace.Part.Parent = workspace
		game.Workspace.Part.CFrame = CFrame.new(10.184, 2.5, -20.017)
		game.Workspace.Part.Name = "Brick1"
	end
end

mouse.KeyDown:connect(onKeyDown)

This script is supposed to create a new part when I press Z (And rename the Part "Brick1" but it returns with the output Part is not a valid member of Workspace AND I DONT KNOW WHY

1 comment

0

You never defined what part was, so on line 9 the script does not know what to parent.

1 answer

✓ Accepted answer

You wrote Instance.new("Part") on line 8 and didn't set its parent, so it isn't in workspace. You're trying to get it from workspace on line 9 so you can set its parent to workspace, which it isn't in.

solution:

Player = game:GetService('Players').LocalPlayer
mouse = Player:GetMouse()

function onKeyDown(key)
    key = key:lower()
    if key == "z" then
        print("Success")
        local part = Instance.new("Part", workspace)
        part.CFrame = CFrame.new(10.184, 2.5, -20.017)
        part.Name = "Brick1"
    end
end

mouse.KeyDown:connect(onKeyDown)

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