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 5 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

01Players = game:GetService("Players")
02Players.PlayerAdded:connect(function(Player)
03 
04    Player.CharacterAdded:connect(function(Character)
05        local Humanoid = Character:WaitForChild("Humanoid")
06        Humanoid.Died:connect(function()
07            if Humanoid:FindFirstChild("creator") ~= nil then
08                local Killer = Humanoid.creator.Value 
09               local Weapon = Killer:FindFirstChildOfClass("Tool") if true then
10                end
11                print (Killer.Name)
12                print (Weapon.Name)
13            end
14        end)
15    end)
16end)
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 — 5y

2 answers

Log in to vote
0
Answered by 5 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:

1local Killer = Humanoid.creator.Value.Character
0
Damn, that tiny error. Thank you so much. jensar141215 157 — 5y
Ad
Log in to vote
0
Answered by 5 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