I’m trying to get the character from the tool when the player equips it so I can attach a motor6d to the right hand, but the character variable is always nil. I tried waiting after the player equips it but that didn’t work, and most of the stuff I found online suggests that this should work.
local tool = script.Parent
local character
local m6d
tool.Equipped:Connect(function()
character = tool.Parent
local a:Weld = character:FindFirstChild("Right Hand"):WaitForChild("RightGrip")
m6d = Instance.new("Motor6D")
m6d.Parent = character:FindFirstChild("Right Hand")
m6d.Name = "RightGrip"
m6d.Part0 = a.Part0
m6d.Part1 = a.Part1
m6d.C0 = a.C0
m6d.C1 = a.C1
a:Destroy()
end)
tool.Unequipped:Connect(function()
m6d:Destroy()
end)
local tool = script.Parent
local character
local m6d
tool.Equipped:Connect(function()
if not tool.Parent:IsA("Model") and not tool.Parent:FindFirstChild("Humanoid") then return end
character = tool.Parent
local a:Weld = character:FindFirstChild("Right Hand"):WaitForChild("RightGrip")
m6d = Instance.new("Motor6D")
m6d.Parent = character:FindFirstChild("Right Hand")
m6d.Name = "RightGrip"
m6d.Part0 = a.Part0
m6d.Part1 = a.Part1
m6d.C0 = a.C0
m6d.C1 = a.C1
a:Destroy()
end)
tool.Unequipped:Connect(function()
m6d:Destroy()
end)
This checks if the tool’s parent is a model, the class of a character, and if it has “Humanoid”.
I just dropped your code into a tool, and it gave me the exact same error. It does find the character model, however there is no child called RightGrip under Right Hand, unless you have manually inserted it from some other script. These are the children inside the Right Hand in an R15 character:
Since you’re stating in your code, it’s a Weld, I believe RightWrist would be what you’re looking for? Or if you’re adding this weld manually, could you share the code, perhaps check if it’s being added?
EDIT: Apparently RightGrip does exist, but it doesn’t show in the workspace, I apologize since I haven’t used it ever before. The only issue in your code is that Right Hand doesn’t have a space in between.
The following code works fine for me:
local tool = script.Parent
local character
local m6d
tool.Equipped:Connect(function()
character = tool.Parent
-- Use "RightHand" instead of "Right Hand"
local a: Weld = character:FindFirstChild("RightHand"):WaitForChild("RightGrip")
m6d = Instance.new("Motor6D")
m6d.Parent = character:FindFirstChild("Right Hand")
m6d.Name = "RightGrip"
m6d.Part0 = a.Part0
m6d.Part1 = a.Part1
m6d.C0 = a.C0
m6d.C1 = a.C1
a:Destroy()
end)
tool.Unequipped:Connect(function()
m6d:Destroy()
end)
IMPORTANT: if you’re doing this all inside a LocalScript it will only affect the client, and not show the changes for other players. I suggest using a RemoteEvent and changing this in the server, if you want it to show for all players.