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

attempt to index field field 'Character' (a nil value)?

Asked by 10 years ago

Can anyone help?

The problem line is 20.

The line is attempt to index field field 'Character' (a nil value).

Character is defined on line 17.

local player = game.Players.LocalPlayer

This script is a LocalScript in a gun in the Backpack.

Someone help.

This is the full script: http://pastebin.com/m2qHmatb

3 answers

Log in to vote
1
Answered by 10 years ago

Add a variable under the player variable like this:

local char = player.Character or player.CharactedAdded:wait()

You can then use that variable for referencing the character, like on line 20:

local torso = char:WaitForChild("Torso") --WaitForChild yields the script until it finds (in this case) Torso in the player's character.
Ad
Log in to vote
0
Answered by
Hibobb 40
10 years ago

You are trying to access the character before it is created

Log in to vote
0
Answered by 7 years ago

Try adding this to it

local Player = game:GetService'Players'.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
0
It's still not working for me I have that code but whenever i click on the part it gives me that error Alexlt10 0 — 4y

Answer this question