I'm trying to create a tool with Instance.new() but it isn't working.
local tool = Instance.new("Tool")
tool.Parent = game.Players.LocalPlayer.Backpack
I'm trying to create a tool with Instance.new() but it isn't working.
local tool = Instance.new("Tool")
tool.Parent = game.Players.LocalPlayer.Backpack
instance.new("ClassName", Parent).
don't be stupid wih it, however. if you need to do costly things to the serialized object, make sure you do it while the object's parent is still nil, like so:
--create a part with a nil parent
local Part = Instance.new('Part');
--set its properties
Part.Color = Color3.fromRGB(150,150,150);
Part.Size = Vector3.new(2,5,2);
Part.CFrame = CFrame.new(0,5,0);
--then parent it
Part.Parent = workspace;
if you just need a basic part for something like testing, then you can simply do
local Part = Instance.new('Part', workspace); --parent object must be a userdata
you can also use Instance.new without pointing a variable, just like any other function:
Instance.new('Part');
just like everything else, be wise with what you do.
the second argument in Instance.new() has been deprecated, and yes, it makes things so much easier, but it's better to set the parent outside because we never know if or when they will remove that functionality entirely.
SteamGOD second argument in Instance.new() is actually not deprecated. Roblox reccomends not to use it due to performance loss but still it can be used.
The 2-argument form of Instance.new(className, parentInstance) is not deprecated, only discouraged in exactly the circumstance Fifkee mentions. You can use it where performance doesn't matter without fear of it being removed; it's one of those things Roblox can't ever remove because it would instantly break nearly all games on the site.
Wait, so they know it causes performance loss, discourage its use, but they won't deprecate it because its been used a lot?
Causes performance issues in specific use cases, not inherently. And yes, there is no real point in deprecating something that can never be removed. If you look at the source of all Roblox places, I bet that more than 99% have at least one occurrence. A change that breaks 90+% of all games on the platform is a change that can't be made.
How do I use Instance.new() in a LocalScript?
The same way you call it in a normal script. Instance.new(ClassName <string>, Parent <Instance>)
Instance library documentation listed below:
https://developer.roblox.com/en-us/api-reference/class/Instance
1] When you call Instance.new on the client, you are creating the instance on the client exclusively. This means the server will not recognize that the new instance because it does not even exist on the server.
2] It is recommended to not use the parent (2nd) argument in Instance.new)
https://devforum.roblox.com/t/psa-dont-use-instance-new-with-parent-argument/30296
If this answers your question, then mark it as the accepted answer.
Since you haven't accepted the previous 2 answers, I'm going to assume you want it done from a local script yet you want all players to see the part, so for that you will need to use a remote event.
regular script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = Instance.new("RemoteEvent", ReplicatedStorage)
remoteEvent.Name = "WhateverYouWantToNameIt"
local function onRemoteEventFired(player,pos) --the player is an included argument, anything after that is what is actually being sent by the local script.
local Part = Instance.new("Part")
Part.Parent = game.Workspace--second arg of Instance.new() is deprecated, do this instead
Part.Position = pos
end
remoteEvent.OnServerEvent:Connect(onRemoteEventFired)
local script inside your tool
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("WhateverYouWantToNameIt")
script.Parent.Activated:connect(function(mouse)
remoteEvent:FireServer(mouse.Hit.Position)
end)
How this works is you will click on the baseplate for example, and it will get the world space where the mouse clicked and send it to the server script where the server script will spawn in a part that everyone on the server can see.
I actually do just want the tool to appear on the client side. I haven't marked any of these as the accepted answer yet because I didn't know there were any answers posted. Thank you for the help, though. :)
Well at least you now know how to make tools FE enabled :)
im guessing your not that experienced in coding yet and probably dont understand much of what the other answers are trying to explain so im ganna make you a sample code and try explaining how to make a tool in a local script as simple as i can.
local tool = Instance.new("Tool", player.Backpack)-- creating the tool and placing in the players backpack
tool.Name = "HittingStick"-- naming the tool
local part = Instance.new("Part", tool) -- creating the physical tool
part.Name = "Handle" -- you have to name the part Handle to make the tool work
part.Shape = Enum.PartType.Cylinder -- shaping the tool
part.Size = Vector3.new(5, .4, .4) -- sizing the tool
part.BrickColor = BrickColor.new("Brown") -- giving the tool color
tool.GripUp = Vector3.new(1, 0.011, 0) -- positioning in hand
tool.GripPos = Vector3.new(-2, 0.1, 0.2) -- positioning in hand
so in this script what i did was first make a tool using Instance.new() with this i made and placed the tool inside of the players backpack. Next i named it HittingStick. I then made a part which would be the actual tool named it Handle placed t in the tool, edited it to my liking's and positioned it.
Although i recommend if your going go use tools in your game have already made tools and place them in replicated storage and then replicate them using a script and place them in the player
tho SteamG0D is correct about using remote events. so place this script in a server script and have it played when a remote event is fired from a local or server script.
This is wrong, it won't work even locally, my guess is this is just your attempt to farm rep as you say that my answer is correct
i actually tested this in my own studio and it worked perfectly but since its being done in a local script other people wont be able to see it so i said you made a point by using remote events and making the tool in a server script would be better.
i dont get why your getting mad over this, im just trying to help somone out.
I am actually slightly experienced in coding. I've just found a little weird bug I'm guessing, because according to all other answers you are able to do it.
i am able to do what?
I mean, I'm able to use Instance.new() in a local script. Sorry
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
Try to be more descriptive when posting a question
What he means is you should be including your script with your question.
Oh, alright then.