Tool Script Error

You can write your topic however you want, but you need to answer these questions:
I’m making a script on pressing W while a weapon is equipped, a different walk animation is played.
The issue is, it’s not working. I’m also getting errors. This local script is inside the tool in StarterPack, by the way.
LOCAL SCRIPT:

local plr = game.Players.LocalPlayer
local Humanoid = plr.Character.Humanoid
local run = Instance.new("Animation")
run.AnimationId = "rbxassetid://5518064511"
local runtrack = Humanoid:LoadAnimation(run)
local bat = workspace.BaseballBat
script.Parent.Equipped:Connect(function()

	game.ReplicatedStorage.ConnectM6D:FireServer(bat)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = game.Players.LocalPlayer.Character.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
if game.StarterPack.Bat.Equipped == true then	
	game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
			runtrack:Play()
		end
	end
		end)
if game.StarterPack.Bat.Equipped == true then
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == Enum.KeyCode.W then 
			runtrack:Stop()
		end
	end
end)

game.StarterPack.Bat.Unequipped:Connect(function()
runtrack:Stop()
				game.ReplicatedStorage.DisconnectM6D:FireServer()
			end)
		end
	end
end)

SERVERSCRIPT:

game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAdded:Connect(function(char)
				
		local M6D = Instance.new("Motor6D", char.Torso)
		M6D.Name = "ToolGrip"
        end)
end)

game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr,location)

      local char = plr.Character
      char.Torso.ToolGrip.Part0 = char.Torso
      char.Torso.ToolGrip.Part1 = location


end)

game.ReplicatedStorage.DisconnectM6D.OnServerEvent:Connect(function(plr)
    plr.Character.Torso.ToolGrip.Part1 = nil
end)
2 Likes

What is the error your getting?

[Players.warycoolio.Backpack.Bat.LocalScript:2: attempt to index nil with ‘Humanoid’]

You need to wait for the character to be loaded in.

Player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
--code here--
end)

CharacterLoaded is not a valid member of Player it says

Hold on, replace Player with what ever your player variable is.

Player.CharacterAdded:Connect(function(char)
local hum = char:WaitForChild("Humanoid")
--code here--
end)

Correction, its CharacterAdded.

I tried that, The bat is not appearing aswell. The remoteevents are listed in my serverscripts above.
LOCALSCRIPT:

local Player = game.Players.LocalPlayer
local run = Instance.new("Animation")
run.AnimationId = "rbxassetid://5518064511"
local bat = workspace.BaseballBat
Player.CharacterLoaded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")	
	local runtrack = Humanoid:LoadAnimation(run)
script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(bat)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = char.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
if game.StarterPack.Bat.Equipped == true then	
	game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
					runtrack:Play()
					print("walking")
		end
	end
		end)
if game.StarterPack.Bat.Equipped == true then
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == Enum.KeyCode.W then 
			runtrack:Stop()
		end
	end
end)

game.StarterPack.Bat.Unequipped:Connect(function()
runtrack:Stop()
				game.ReplicatedStorage.DisconnectM6D:FireServer()
			end)
		end
	end
	end)
end)

Ohh, First off tools are not stored in the starterpack, they go into the players backpack from starterpack,
and put the localscript inside the tool, so that way you can just do script.Parent.Equipped:Connect(function())

end)

Okay. I edited it, however same error. CharacterLoaded is not a valid member of player

local Player = game.Players.LocalPlayer
local run = Instance.new("Animation")
run.AnimationId = "rbxassetid://5518064511"
local bat = workspace.BaseballBat
Player.CharacterLoaded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")	
	local runtrack = Humanoid:LoadAnimation(run)
script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(bat)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = char.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
		if script.Parent.Equipped == true then
			game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
					runtrack:Play()
					print("walking")
		end
	end
		end)
if script.Parent.Equipped == true then
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == Enum.KeyCode.W then 
			runtrack:Stop()
		end
	end
end)

script.Parent.Unequipped:Connect(function()
runtrack:Stop()
				game.ReplicatedStorage.DisconnectM6D:FireServer()
			end)
		end
	end
	end)
end)

No, its not CharacterLoaded, its CharacterAdded.
Change this

Player.CharacterLoaded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")	
	local runtrack = Humanoid:LoadAnimation(run)
script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(bat)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = char.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
		if script.Parent.Equipped == true then
			game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
					runtrack:Play()
					print("walking")
		end
	end
		end)

to this.

Player.CharacterAdded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")	
	local runtrack = Humanoid:LoadAnimation(run)
script.Parent.Equipped:Connect(function()
	game.ReplicatedStorage.ConnectM6D:FireServer(bat)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = char.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
		if script.Parent.Equipped == true then
			game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
					runtrack:Play()
					print("walking")
		end
	end
		end)

The animation doesn’t load, but the bat does exist

Oh wait, please make sure your animation priority is set correctly, having too low priority, could just stop the animation from playing completely.

I set it to Action, and it still doesn’t work

Uhh, no I think it needs to be set as movement for this one.

Tried both, and it still does not load the animation,
LOCAL SCRIPT:

local bat = game.Workspace.BaseballBat
local Player = game.Players.LocalPlayer
local run = Instance.new("Animation")
run.AnimationId = "rbxassetid://5522808586"
Player.CharacterAdded:Connect(function(char)
local Humanoid = char:WaitForChild("Humanoid")	
	local runtrack = Humanoid:LoadAnimation(run)
script.Parent.Equipped:Connect(function()
		game.ReplicatedStorage.ConnectM6D:FireServer(bat)
		wait(1)
game.Players.LocalPlayer.Character:WaitForChild("ToolGrip")
	game.Players.LocalPlayer.Character.Torso.ToolGrip.Part0 = char.Torso
	game.Players.LocalPlayer.Character.ToolGrip.Part1 = bat
		if script.Parent.Equipped == true then
			game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
	if not gameProcessed then 
		if Input.KeyCode == Enum.KeyCode.W then 
					runtrack:Play()
					print("walking")
		end
	end
		end)
if script.Parent.Equipped == true then
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed)
	if not gameProcessed then
		if Input.KeyCode == Enum.KeyCode.W then 
			runtrack:Stop()
		end
	end
end)

script.Parent.Unequipped:Connect(function()
runtrack:Stop()
				game.ReplicatedStorage.DisconnectM6D:FireServer()
			end)
		end
	end
	end)
end)

Also, the bat does not disappear after I unequip it, since that happens, i’ll show you the remote events aswell.

game.Players.PlayerAdded:Connect(function(plr)			
	plr.CharacterAdded:Connect(function(char)
				
		local M6D = Instance.new("Motor6D", char.Torso)
		M6D.Name = "ToolGrip"
        end)
end)

game.ReplicatedStorage.ConnectM6D.OnServerEvent:Connect(function(plr,location)

      local char = plr.Character
      char.Torso.ToolGrip.Part0 = char.Torso
      char.Torso.ToolGrip.Part1 = location


end)

game.ReplicatedStorage.DisconnectM6D.OnServerEvent:Connect(function(plr)
    plr.Character.Torso.ToolGrip.Part1 = nil
end)

Ok so since your trying to do a walk animation with the roblox character. You have to change the default animation. This has to been done with in a server script, so this just got even more complex.

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		wait(3)
		char.Animate.walk.WalkAnim.AnimationId = "rbxassetid://YOURANIMATIONID"
		char.Animate.run.RunAnim.AnimationId = "rbxassetid://YOURANIMATIONID"
	end)
end)

It now does work, problem is the Bat does not disappear on Unequip and the walk animations aren’t reverted, If you need a video, I can record it for you.

@warycoolio If you want the walk animations reverted, try using a remoteevent to make this work, and yes please record a video.

The bat was also not showing when I equipped it

External Media

Ok, how about when you want the animation to work, fire a remote event, and when your done with it fire that same remoteevent again and just change the stuff there.and how about just making the bat invisible when its unequipped.