Attempt to index boolean with 'Play' -- Error

Hello, Developers!

I was making a tool that had animations that showed when it was activated and if it was activated then it would attack. I came across an error when I was attacking and unequipping the tool. It was attemp to index boolean with ‘Play’ I tried to mess around with it to see if it worked but it still gave me the same error.

Code:

local player = game.Players.LocalPlayer
local Character = player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator") 
local idle = Animator:LoadAnimation(script.Animations:WaitForChild("Idle"))
local activate = Animator:LoadAnimation(script.Animations:WaitForChild("Activate"))
local attack = Animator:LoadAnimation(script.Animations:WaitForChild("Attack"))
local UIS = game:GetService("UserInputService")
local canAttack = false
local activated = false
local isactivating = false
local stunBlock = script.Parent.Stun.ParticleEmitter
stunBlock.Enabled = false

script.Parent.Equipped:Connect(function()
	idle:Play()
	UIS.InputBegan:Connect(function(input, gameprocess)
		if not gameprocess then
			if input.KeyCode == Enum.KeyCode.X and  activated == false then
				print("activated")
				activated = true
				canAttack = true
				if isactivating == false then 
					isactivating = true
					activate:Play() --- Error
					activate.Stopped:Wait()
					isactivating = false
				end
				stunBlock.Enabled = true
				stunBlock.Parent = script.Parent.Stun
			elseif input.KeyCode == Enum.KeyCode.X and activated == true then
				print("deactivated")
				activated = false
				canAttack = false
				if isactivating == false then
					isactivating = true
					activate:Play() --- I think if I fixed the error above this would error as well.
					activate.Stopped:Wait()
					isactivating = false
				end
				stunBlock.Enabled = false
				stunBlock.Parent = nil
			end
		end	
	end)
end)



script.Parent.Activated:Connect(function()
	if canAttack == true and activated == true then
		canAttack = false
		attack:Play()
		script.Parent.Stun.Touched:Connect(function(hit)
			if canAttack == true then
				canAttack = false
				if hit.Parent:FindFirstChild("Humanoid") then
					hit.Parent.Humanoid:TakeDamage(5)
				end
				canAttack = true
			end
		end)
		attack.Stopped:Wait()
		wait()
		attack:Stop()
		canAttack = true
	end
end)

script.Parent.Unequipped:Connect(function()
	canAttack = false
	activate = false
	stunBlock.Enabled = false
	stunBlock.Parent = nil
	idle:Stop()
end)

Thank you,

Ultan

When you unequip the tool, activate, your animation variable, is set to false, did you mean to set activated to false?

1 Like