Attempt to index index nil with 'Torso'

I’m trying to create a baseball bat that you can attack with.
I’ve searched alot to find the solution to it, but I didn’t find anything that could help.

local char = game.Players.LocalPlayer.Character
local WeaponTool = script.Parent

script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
	
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = WeaponTool.Weapon

end)

WeaponTool.Unequipped:Connect(function()

	game.ReplicatedStorage.DisconnectM6D:FireServer()

end)

When you call local “char” your character hasn’t loaded in yet. One simple fix is replacing “game.Players.LocalPlayer.Character” with game.Players.LocalPlayer:WaitForChild(“Character”)

It might be coming up as nil if you’re using an R15 plr.

Character isnt a child of player so that wouldnt work
the solution is:
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

the character being nil is causing the error not the torso

Ah well in that case like @kylerzong said use

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

And if you still can’t collect the torso then use

local Torso = char:WaitForChild('Torso')

None of these seem to work.rrrrrrrrrrrr

It still does not seem to work as intended.

No, there’s another script inside of it. The current one giving errors is a LocalScript.

This should work its what i do.

repeat wait() until game.Players.LocalPlayer.Character
local char = game.Players.LocalPlayer.Character
repeat wait() until char.Torso
local WeaponTool = script.Parent

script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
	
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = WeaponTool.Weapon

end)

WeaponTool.Unequipped:Connect(function()

	game.ReplicatedStorage.DisconnectM6D:FireServer()

end)

Still not working, maybe I will just use a melee kit and that’s it.

Also thanks to everyone for the help.

So maybe this can work, place it to Script (Not local script)

local Players = game:GetService("Players")
local WeaponTool = script.Parent

script.Parent.Equipped:Connect(function()

local Char = WeaponTool.Parent
local Player = Players:GetPlayerFromCharacter(Char)

game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)

Char.Torso.ToolGrip.Part0 = Char.Torso
Char.Torso.ToolGrip.Part1 = WeaponTool.Weapon

end)

WeaponTool.Unequipped:Connect(function()

game.ReplicatedStorage.DisconnectM6D:FireServer()

end)

Not sure if you can call :FireServer() through a server script though

Oof i remember that [30 CHAAAAAARS]

But the main logic is using this:

local Players = game:GetService("Players")
local Char = WeaponTool.Parent
local Player = Players:GetPlayerFromCharacter(Char)

So he can edit that.
(Maybe using this: game.ReplicatedStorage.ConnectM6D:FireAllClients(WeaponTool.Weapon))

Yes, you’re right, he can use that to his desire.

I first want to bring up the point that “Torso” doesn’t exist within R15 character models, thus, you should ensure that you’re testing this script with R6 character models only.

I also want to bring up that “ToolGrip” by default doesn’t exist in Torso, so I can only assume that this instance is being created by the ConnectM6D Remote Event.

An idea I have is to use empty variables and declare them when Equipped is called on the tool. I also want to encourage using print statements to determine where the bug is.

local WeaponTool = script.Parent
local player
local char -- Blank Variable that will be Assigned Later (if still nil)

script.Parent.Equipped:Connect(function()
	print("Weapon Equipped")

	if char == nil then
		player = game:GetService("Players").LocalPlayer
		print(player.Name .. " owns the weapon!")

		char = player.Character or player.CharacterAdded:Wait()
		print(char.Name .. "'s character has been found!")
	end -- Assigns Player And Character Variables

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
	
	local torso = char:FindFirstChild("Torso")
	if torso then 
		print("Torso Exists")
	else
		print("NO TORSO FOUND")
		return
	end -- Searches For Torso

	local ToolGrip = torso:FindFirstChild("ToolGrip")
	if ToolGrip then
		print("ToolGrip Found")
	else
		print("ToolGrip NOT FOUND")
		return
	end -- Searches for Tool Grip

	ToolGrip.Part0 = torso
	ToolGrip.Part1 = WeaponTool.Weapon -- Sets Part0 and Part1
end)

WeaponTool.Unequipped:Connect(function()
	game.ReplicatedStorage.DisconnectM6D:FireServer()
end)

Obviously, all the prints are for debuggins purposes, and once a clear solution comes, you can remove these checks.

Add this to the top of your code```Lua
repeat wait() until game.Players.LocalPlayer.Character == true

1 Like
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

You have to wait for it:

local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local WeaponTool = script.Parent

script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(WeaponTool.Weapon)
	
	char.Torso.ToolGrip.Part0 = char.Torso
	char.Torso.ToolGrip.Part1 = WeaponTool.Weapon

end)

WeaponTool.Unequipped:Connect(function()

	game.ReplicatedStorage.DisconnectM6D:FireServer()

end)