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

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

Asked by 8 years ago
Edited 8 years ago
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

0
You never defined what part was, so on line 9 the script does not know what to parent. M39a9am3R 3210 — 8y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago
Edited 8 years ago

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)

Ad

Answer this question