So, I’m trying to make something happen when a tool is equipped but I get an error that says
PlayerScripts.LocalScript:18: attempt to index nil with ‘Equipped’
Here’s my script:
local player = game.Players.LocalPlayer
local katana = player:WaitForChild("Backpack"):FindFirstChild("Katana")
local char = player.Character
if not char or not char.Parent then
char = player.CharacterAdded:wait()
end
local Humanoid = char:WaitForChild("Humanoid")
local animator = Humanoid:WaitForChild("Animator")
if char then
katana.Equipped:Connect(function()
print("Equipped")
end)
end
might not solve the question but you can shorten this into
local char = player.Character or player.CharacterAdded:Wait()
also, use RBXScriptSignal:Wait() instead of RBXScriptSignal:wait() (same with :Connect and :Disconnect), because the lower case version is (from what i heard) deprecated meaning it shouldn’t be used in new work.
also, the if char then in line 13 is useless now.
now to solve your question maybe change
local katana = player:WaitForChild("Backpack"):FindFirstChild("Katana")
to
local katana = player:WaitForChild("Backpack"):WaitForChild("Katana")
because server replicating to client takes time
fixed script:
local plr = game:GetService("Players").LocalPlayer -- use game:GetService(Service) for services, workspace if the service is the Workspace Service. game.Workspace is deprecated.
local katana = plr:WaitForChild("Backpack"):WaitForChild("Katana")
local chr = plr.Character or plr.CharacterAdded:Wait() -- use :Wait() instead of :wait()
local hum = chr:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
katana.Equipped:Connect(function()
print("Equipped")
end)