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

How do I use Instance.new() in a LocalScript? [closed]

Asked by
sngnn 274 Moderation Voter
4 years ago
Edited 4 years ago

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
0
Try to be more descriptive when posting a question EpicMetatableMoment 1444 — 4y
0
What he means is you should be including your script with your question. SteamG00B 1633 — 4y
0
Oh, alright then. sngnn 274 — 4y

Closed as Non-Descriptive by EpicMetatableMoment, SteamG00B, and hiimgoodpack

This question has been closed because its title or content does not adequately describe the problem you are trying to solve.
Please ensure that your question pertains to your actual problem, rather than your attempted solution. That is, you were trying to solve problem X, and you thought solution Y would work, but instead of asking about X when you ran into trouble, you asked about Y.

Why was this question closed?

4 answers

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago
Edited 4 years ago

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.

1
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. SteamG00B 1633 — 4y
0
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. karlo_tr10 1233 — 4y
0
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. EmilyBendsSpace 1025 — 4y
0
Wait, so they know it causes performance loss, discourage its use, but they won't deprecate it because its been used a lot? SteamG00B 1633 — 4y
0
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. EmilyBendsSpace 1025 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Question

How do I use Instance.new() in a LocalScript?

Answer

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

Potential Problems

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

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
4 years ago
Edited 4 years ago

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.

0
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. :) sngnn 274 — 4y
1
Well at least you now know how to make tools FE enabled :) SteamG00B 1633 — 4y
Log in to vote
-1
Answered by 4 years ago

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

0
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. ahsan666666666666 264 — 4y
0
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 SteamG00B 1633 — 4y
0
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. ahsan666666666666 264 — 4y
0
i dont get why your getting mad over this, im just trying to help somone out. ahsan666666666666 264 — 4y
View all comments (3 more)
0
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. sngnn 274 — 4y
0
i am able to do what? ahsan666666666666 264 — 4y
0
I mean, I'm able to use Instance.new() in a local script. Sorry sngnn 274 — 4y