Why isn't this code working?

I am trying to allow the player to spawn a part with the assigned keybind to it. (I know I didn’t assign the location for the part to spawn at)
image

1 Like

The problem with your code is that it’s in a script, not a local script. Local scripts give access to LocalPlayer, while regular scripts do not. Regular scripts run on the server, local scripts run on the client. So in general when you want the client to do something, it should be a local script, if you want the server to do something, it should be a regular script.

1 Like

I did that, and it still doesn’t work. It launches and doesn’t error, but my code won’t work.

If you don’t give the part a position, it will be created at 0, 0, 0. Are you sure that the part is not just there? You can check in the explorer to see if the part actually exists in workspace.

You should also create a Part variable, so that you can easily define its properties that way

I’d also recommend not using the second parameter for Instance.new:

Put the LocalScript inside StarterPlayerScripts, and insert this:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.Button1Down:Connect(function()
    local Part = Instance.new("Part")
    --Assign your properties here
    Part.Parent = workspace
end)

I am quite certain. I know it spawns at 0,0,0, but nothing appears.

Nothing appears in the explorer or in the world? Since 0, 0, 0 is inside of the baseplate if you’re testing on a baseplate.

Still didn’t work, sorry. Would assigning it a position affect it?

Yes, I moved the baseplate down like 50 studs, and nothing appeared in the air or ground.

I don’t think it’d be the position though? If it is, try setting the Position property to

Part.Position = Vector3.new(25, 25, 0)

You can also try debugging it with print() statements so that we know the code fully works

1 Like

Ah apologies. After testing I figured out the problem. You cannot have a local script in the workspace, since that’s meant as a place to render physical things and is mainly for the server. If you move the local script to StarterPlayerScripts it should work as intended.

1 Like

It works! My script is functional, thanks to you. :grin: