Hi, I want to script a tool from the outside (script inside StarterPlayerScripts). How can I reference my tool and connect to it’s functions? Usually tool scripts are a child of the tool, and the tool is referenced using script.Parent
. I prefer to place my scripts inside of StarterPlayerScripts to make them more easily accessible. I’m sure this is a simple question but I can’t seem to figure it out.
You can just point a variable to the tool location:
local myTool=workspace.GameTools.MyTool
The way to do this chief is to use whats called a object value and manually link it by clicking on it from starter tool
I hate to be that guy on the forums but I think a more proper statement would be you could just create a variable to the tools location
Yeah, don’t be that guy, nobody likes that guy
But yeah you are technically correct
The tool isn’t in the workspace, but in StarterPack. It then gets cloned to every player’s Backpack. I can’t reference the tool from a LocalPlayer’s Backpack…
Let me know if this works.
local tool = script.Parent.Parent:WaitForChild("BackPack"):WaitForChild("ToolName")
Sent from mobile.
I mean you could do something like this.
local player = game.Players.LocalPlayer
local Backpack = player:WaitForChild("Backpack")
Backpack.ChildAdded:Connect(function(child)
if child:IsA("Tool") then
--code here
child.Activated:Connect(function()
print("Activated")
end)
end
end)
Assuming what you mean by ‘connect to its functions’ is using a tool’s events to connect to tool functions, just make a variable in your local script that references the tool:
local tool = --location of tool
tool.Equipped:Connect(function()
--code here
end)
tool.Unequipped:Connect(function()
--code here
end)
Doesn’t work. The script infinitely yields.
The ChildAdded event doesn’t fire if I place the tool inside StarterPack.
wait, are you using a local script? because you have to
Try this?
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
print("[Client] Script Online")
Character.ChildAdded:Connect(function(NewChild)
if NewChild:IsA("Tool") then
local Tool = NewChild
print("[Client] Found a tool, waiting for a event to fire")
Tool.Activated:Connect(function()
print("[Client] Activated the Tool from outside the tool object")
end)
end
end)
Yes I am.
I’m not having trouble connecting to the tool’s events, I’m having trouble referencing the tool.
this is likely ur problem
if it’s not already, put the script inside StarterCharacterScripts
and not StarterPlayerScripts
, then reference the tool like so:
local user = game.Players.LocalPlayer
user.CharacterAdded:Connect(function()
local tool = --tool location
tool.Equipped:Connect(function()
print('equipped')
end)
tool.Unequipped:Connect(function()
print('unequipped')
end)
end
Can you show me where your script is exactly located. Is it inside the StarterPlayerScripts
folder, or is it inside a folder in the StarterPlayerScripts
folder?
The script is parented to StarterPlayerScripts. With no folders in between.
My bad… It’s Backpack
, not BackPack
.
local tool = script.Parent.Parent:WaitForChild("Backpack"):WaitForChild("ToolName")
Remember to replace ToolName with the name of your tool!
If I understood correctly, you want to obtain the tool from a LocalScript that is not inside it and there create functions, when a player equips a tool it is put inside its character, what it should do is:
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Tool = Character.ToolName
And from the variable “Tool” do the functions.
Why reference the character if you’re not using it?
Also, using :WaitForChild()
is better because you’re sure it won’t error.
and this is kinda unrelated but im actually addicted to :WaitForChild()
i even use it in server scripts lol