Attempt to index nil with 'Play'

Local Script:

local uis = game:GetService("UserInputService")
local tool = script.Parent
local anim = tool:WaitForChild("Throw")
local track = nil
local event = tool:WaitForChild("ThrowingEvent")
local equipped = false
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()

tool.Equipped:Connect(function()
	equipped = true
	local char = tool.Parent
	local humanoid = char:WaitForChild("Humanoid")
	local track = humanoid.Animator:LoadAnimation(anim)
	
	mouse.Button1Down:Connect(function()
		event:FireServer()
	end)
end)

tool.Unequipped:Connect(function()
	equipped = false
end)

uis.InputBegan:Connect(function(input)
	if equipped and input.UserInputType == Enum.UserInputType.MouseButton1 then
		track:Play()
	end
	
	if equipped and input.UserInputType == Enum.UserInputType.Touch then
		track:Play()
	end
end)

Script:

local tool = script.Parent
local event = script.Parent:WaitForChild("ThrowingEvent")
local debris = game:GetService("Debris")

event.OnServerEvent:Connect(function(player)
	local dynamite = tool.Handle:Clone()
	dynamite.Parent = workspace
	dynamite.CanCollide = true
	local char = player.Character or player.CharacterAdded:Wait()
	local hrp = char:WaitForChild("HumanoidRootPart")
	local dir = hrp.CFrame.LookVector + hrp.CFrame.UpVector
	dynamite.VectorForce.Force = dir * 200
	tool:Destroy()
	wait(0.2)
	dynamite.VectorForce.Enabled = false
	wait(1.5)
	dynamite.ExploseSound:Play()
	
	local vfx = Instance.new("Explosion", workspace)
	vfx.Position = dynamite.Position
	vfx.BlastRadius = 20
	vfx.BlastPressure = 500000
	dynamite.Transparency = 1
	dynamite.Anchored = true
	dynamite.CanCollide = false
	debris:AddItem(dynamite, 2.2)
	
	
end)

So here I have a script to throw dynamite, but only for some reason that appears here is the error, why?

the script is telling you that the track variable is still nil, so add an if statement to detect if track isn’t nil

if track ~= nil then
	track:Play()
end

Or, just set the track variable immediatly instead :confused:

local uis = game:GetService("UserInputService")
local tool = script.Parent
local anim = tool:WaitForChild("Throw")
local event = tool:WaitForChild("ThrowingEvent")
local equipped = false
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

local animator = hum:FindFirstChild("Animator") or Instance.new("Animator", hum)

local track = animator:LoadAnimation(anim)

Also please use .Activated except of overcomplicating the code with if equipped and inputs

Well thanks for the answer, but do you happen to know why I have dynamite flying like this:

he either moves away or flies too far away. What is the problem I wanted it to fall near the player.

setting it to *200 is very too much, also i’d prefer using BodyVelocity or LinearVelocity

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.