Players.squaredsnow.Backpack.Da Whip.Script:17: attempt to index nil with 'Character'

Me again!

I added an animation onto my tool but they don’t stop after its unequipped. And the sound doesn’t stop after its unequipped either.

heres my script:

local tool = script.Parent



tool.Equipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
	local char = plr.Character
	local anim = tool.Idle
	local idle = char:WaitForChild("Humanoid"):LoadAnimation(anim)
	idle:Play()
	
	tool.Handle.IdleMusic:Play()
end)

tool.Unequipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
	local char = plr.Character
	local anim = tool.Idle
	local idle = char:WaitForChild("Humanoid"):LoadAnimation(anim)
	idle:Stop()
	
	tool.Handle.IdleMusic:Stop()
end)

The sound just keeps playing after. Another issue I have is that whenever I equip the tool I always spawn in one place. I have the tool unanchored but cancollide on so I wouldn’t know why I’m spawning in the place where it was in workspace after I put it in starterpack.

local char = tool.Parent

You already have the character, you don’t need GetPlayerFromCharacter.

The problem is that it is inside the backpack when unequipped and it is trying to get a player from the player’s backpack, to fix, simply reference the player like:

tool.Unequipped:Connect(function()
	local plr = tool.Parent.Parent
	local char = plr.Character
	local anim = tool.Idle
	local idle = char:WaitForChild("Humanoid"):LoadAnimation(anim)
	idle:Stop()
	
	tool.Handle.IdleMusic:Stop()
end)
1 Like

The animation doesn’t play anymore, neither does the sound, and it gives me this error:

Infinite yield possible on 'Players.squaredsnow.Backpack:WaitForChild("Humanoid")'

Character is not a valid member of Workspace “Workspace”

So it sometimes runs when the tool is under the character or backpack, so change it to reference either.

tool.Unequipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent) or tool.Parent.Parent
	local char = plr.Character
	local anim = tool.Idle
	local idle = char:WaitForChild("Humanoid"):LoadAnimation(anim)
	idle:Stop()
	
	tool.Handle.IdleMusic:Stop()
end)

Now nothing plays at all. But no errors.

local tool = script.Parent
local idle


tool.Equipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
	local char = plr.Character
	local anim = tool.Idle
	idle = char:WaitForChild("Humanoid"):LoadAnimation(anim)
	idle:Play()
	
	tool.Handle.IdleMusic:Play()
end)

tool.Unequipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
	local char = plr.Character
	local anim = tool.Idle
        if idle ~= nil then
	idle:Stop()

        end 
	
	tool.Handle.IdleMusic:Stop()
end)

I made the idle track a variable. Try the original script but with the idle track as a variable

Animation still plays. So does the sound. And I keep spawning at a certain place still as well. Which I still need to fix

tool.Unequipped:Connect(function()
	local plr = game.Players:GetPlayerFromCharacter(tool.Parent)
print("Got player", plr)
	local char = plr.Character
print("Got char", char)
	local anim = tool.Idle
        if idle ~= nil then
print("Inside if statement")
	idle:Stop()

        end 
	print(tool.Handle.IdleMusic)

	tool.Handle.IdleMusic:Stop()
print("stopped the music")
end)

Add printstatements. send the output after.

1 Like

Nothing printed, also everything just stopped working. I can only equip it, and theres no errors at all.

Is this a local or serverscript? Try adding a print statement at the very beginning of the script. Does print it?

serverscript. everything happens serversided. Also yes it printed something but when i do it for everything else it doesnt work

What about when it is equipped? Try adding print statements on the equipped event

I’m gonna try to re-write my script. This time with a RemoteEvent

That should work, I think you can only call Tool.Equipped on the client and same goes for Unequipped

So for some reason my script wont stop the animation, everything else stops though. And I can’t seem to figure out how to stop my tool from taking me to the place the tool was in while it was in workspace

Issues:

-Tool doesnt stop playing animation

-after I finished the tool, i put it in starterpack. Now whenever I use it, I spawn in the place the tool was at in workspace. Its not anchored, cancollide is on too.

Heres my serverscript for stopping the animation:

local tool = script.Parent
local event = tool:WaitForChild("Unequipped")

event.OnServerEvent:Connect(function(player)
	local char = player.Character
	local anim = tool:WaitForChild("Idle")
	local ContentProvider = game:GetService("ContentProvider")
	local idle  = player.Character:WaitForChild("Humanoid"):LoadAnimation(anim)
	ContentProvider:PreloadAsync({idle})
	idle:Stop()

	local sound = tool.Handle.IdleMusic
	sound:Stop()
end)

and heres my local script I used to trigger the event:

local tool = script.Parent
local player = game.Players.LocalPlayer
tool.Unequipped:Connect(function(player)
	local event = tool:WaitForChild("Unequipped")
	event:FireServer()
end)

this track are separated this cant be stop and you should load your animation once

local Tool = script.Parent
local Player = script:FindFirstAncestorWhichIsA("Player") or game.Players:GetPlayerFromCharacter(Tool.Parent)
local Character = Player.Character

local Idle = Character:WaitForChild("Humanoid"):LoadAnimation(Tool.anim)

Tool.Equipped:Connect(function()
	Idle:Play()
	Tool.Handle.IdleMusic:Play()
	Tool.Unequipped:Wait()
	Idle:Stop()
	Tool.Handle.IdleMusic:Stop()
end)

Not to offend anyone, but this script kinda made my script worse. Which I don’t know whats happening in your script, and also I’m trying to stop my animation but it doesn’t do that, and it makes the sound keep playing too.

local tool = script.Parent
local handle = tool.Handle
local idleSound = handle.IdleMusic
local anim = tool.Idle
local idleTrack = nil

local debounce = false
local touchedConnection = nil

tool.Equipped:Connect(function()
	local character = tool.Parent
	local humanoid = character.Humanoid
	humanoid.WalkSpeed = 45
	
	if not idleTrack then
		idleTrack = humanoid:LoadAnimation(anim)
	end
	
	repeat task.wait() until idleTrack.Length > 0
	idleTrack:Play()
	idleSound:Play()

	touchedConnection = handle.Touched:Connect(function(hit)
		if debounce then
			return
		end

		local hitModel = hit:FindFirstAncestorOfClass("Model")
		if hitModel then
			local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
			if hitHuman then
				debounce = true
				hitHuman:TakeDamage(math.huge)
				task.wait(0.25)
				debounce = false
			end
		end
	end)
end)

tool.Unequipped:Connect(function()
	if touchedConnection then
		touchedConnection:Disconnect()
		touchedConnection = nil
	end

	local player = tool:FindFirstAncestorOfClass("Player")
	local character = player.Character
	local humanoid = character.Humanoid
	humanoid.Walkspeed = 16
	idleSound:Stop()
	if idleTrack then
		idleTrack:Stop()
	end
end)