So I am currently wanting to make a gui which if you click the Place Part button then it places a part at the players position. But when I click the Place Part button it shows this in the output:
Players.xxoplol47811t.PlayerGui.ScreenGui.Frame.PlacePartButton.LocalScript:7: invalid argument #2 to 'new' (Instance expected, got Vector3)
Also I probably use debounce weird compared to other scripters but I prefer this way.
Code which is in a local script in a button:
local button = script.Parent local Position = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position debounce = false script.Parent.MouseButton1Click:Connect(function() if debounce == false then Instance.new("Part", Position) debounce = true else if debounce == true then wait(0.5) debounce = false end end end)
Thanks.
The others explained your question (by the way, Roblox discourages using the 2nd parameter to parent something, as it isn't as efficient as assigning the Position first and then parenting it); I'm going to explain every other problem you may experience after fixing it:
1) When you click the button, it won't spawn the part in the correct position. This is because Vector3s, strings, numbers, and other basic values "copy", whereas tables/instances do not. Illustration:
local a = 1 local b = a -- this only copies the value of "a" (currently 1) into "b" b += 1 -- this only alters the value of "b" print(a, b) -- 1, 2
Compared to:
local a = Instance.new("Part") print(a.Name) -- Part local b = a -- This copies a *reference* to the Part (you still only have one Part); now both variables refer to the same object b.Name = "PartB" -- This changes the name of the object print(a.Name, b.Name) -- PartB PartB -- But note that if you assign to the entire variable "b", you aren't altering "a": b = Instance.new("Seat") print(a.Name, b.Name) -- PartB Seat
To fix this, access the .Position
of the HumanoidRootPart in the MouseButton1Click handler, not before
2) Your second click won't spawn a part, even if it was 10 seconds after the first click. Think through your debounce implementation and what happens each step of the way after clicking on the button. Your script won't start wait
ing until you click for the 2nd time.
Improved script (I'm assuming it's in in a GUI that respawns when the player does):
local button = script.Parent local root = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart") local debounce = false script.Parent.MouseButton1Click:Connect(function() if debounce then return end local part = Instance.new("Part") part.Position = root.Position part.Parent = workspace debounce = true wait(0.5) debounce = false end)
if Position then Instance.new("Part").Position = Position end
for line 7. youre parenting the part to the position bruh
Try this:
local button = script.Parent --[[ old local Position = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart").Position -- new ]] local HRP = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart") debounce = false script.Parent.MouseButton1Click:Connect(function() if debounce == false then local P = Instance.new("Part", workspace) -- should be put into the workspace and not the posiiton since this was causing the error P.Position = HRP.Position -- correct way to position a part (also changed the above Position variable to reference the `HumanoidRootPart` then only call the position when you click on this. This is done so it will update the position of the character. The old way would only update the position once the script has loaded and wont be on the character unless the character or player stays still) debounce = true else if debounce == true then wait(0.5) debounce = false end end end)
Hope this helps!
Instead of this
Instance.new("Part", Position)
try this:
local Part = Instance.new("Part", game.Workspace) Part.Position = Position