Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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)
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. Just2Terrify 566 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

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
0
Damn, that tiny error. Thank you so much. jensar141215 157 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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!

Answer this question