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

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

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

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.

You are trying to access the character before it is created

Try adding this to it

local Player = game:GetService'Players'.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

1 comment

0

It's still not working for me I have that code but whenever i click on the part it gives me that error

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