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.
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.
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
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?