Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I fix "Attempt to index local 'Weapon' (a nil value)"?

Hello, so I'm currently making a script that prints out

  1. Killer Name
  2. Killer Weapon

Whenever I go to test it I get an error in my output saying Workspace.Script:12: attempt to index local 'Weapon' (a nil value) How would I go about fixing this? Here's my full script

Players = game:GetService("Players") 
Players.PlayerAdded:connect(function(Player) 

    Player.CharacterAdded:connect(function(Character) 
        local Humanoid = Character:WaitForChild("Humanoid") 
        Humanoid.Died:connect(function() 
            if Humanoid:FindFirstChild("creator") ~= nil then 
                local Killer = Humanoid.creator.Value  
               local Weapon = Killer:FindFirstChildOfClass("Tool") if true then
                end 
                print (Killer.Name)
                print (Weapon.Name) 
            end 
        end)
    end)
end)

1 comment

0

When it posts that something is nil, that means there is no value with it... a good example being if it's suppose to show the weapon that killed it, it most likely wasn't able to read the tools name.

2 answers

✓ Accepted answer

I'm not too familiar with the creator tag but I believe the issue is that creator.Value is the actual player rather than the player's character. Try adding .Character to the end of line 8 like so:

local Killer = Humanoid.creator.Value.Character

1 comment

0

Damn, that tiny error. Thank you so much.

The player's humanoid has a creator value, what type of variable is it?
If it's an object value, then it's referring to the player in game.Player The way to fix it is to add a string value and set it as the killer, so replace it with workspace[creator.Value]
Hoped this helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.