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

why does the output say that "Character" is a nil value?

Asked by 5 years ago

the script that i'm making is supposed to clone each body part, however before even starting to code this script the output says "attempt to index local 'Character' (a nil value)"

what is wrong with this script? i've tried multiple solutions with no outcome.

-- transform script

local player = game.Players.LocalPlayer
local mouse = script.Parent.Parent:GetMouse()
local Character = player.Character 

local transformed = Instance.new("BoolValue")
transformed.Name = "Transformed"
transformed.Value = false
transformed.Parent = script

-- body parts

local LUarm = Character.LeftUpperArm
local LLarm = Character.LeftLowerArm
local Lhand = Character.LeftHand
local RUarm = Character.RightUpperArm
local RLarm = Character.RightLowerArm
local Rhand = Character.RightHand
local utorso = Character.UpperTorso
local ltorso = Character.LowerTorso
local LUleg = Character.LeftUpperLeg
local LLleg = Character.LeftLowerLeg
local Lfoot = Character.LeftFoot
local RUleg = Character.RightUpperLeg
local RLleg = Character.RightLowerLeg
local Rfoot = Character.RightFoot
local head = Character.Head
local hroot = Character.HumanoidRootPart

local CLUarm = Character.LeftUpperArm:Clone()
local CLLarm = Character.LeftLowerArm:Clone()
local CLhand = Character.LeftHand:Clone()
local CRUarm = Character.RightUpperArm:Clone()
local CRLarm = Character.RightLowerArm:Clone()
local CRhand = Character.RightHand:Clone()
local Cutorso = Character.UpperTorso:Clone()
local Cltorso = Character.LowerTorso:Clone()
local CLUleg = Character.LeftUpperLeg:Clone()
local CLLleg = Character.LeftLowerLeg:Clone()
local CLfoot = Character.LeftFoot:Clone()
local CRUleg = Character.RightUpperLeg:Clone()
local CRLleg = Character.RightLowerLeg:Clone()
local CRfoot = Character.RightFoot:Clone()
local Chead = Character.Head:Clone()
local Chroot = Character.HumanoidRootPart:Clone()

0
Every answer doesn't work. This is amazing. DeceptiveCaster 3761 — 5y
0
 Check my answer. I have tested it, and its fully functional. Thetacah 712 — 5y

4 answers

Log in to vote
0
Answered by
Thetacah 712 Moderation Voter
5 years ago
Edited 5 years ago

Hi, Your script is being run before the Character spawns in the game, as such, Character is a nil value. To fix this, you should wait until the players character is added into the game like so:

local character = player.Character or player.CharacterAdded:wait()

Full code:

-- transform script

-- transform script

local player = game.Players.LocalPlayer
local mouse = script.Parent.Parent:GetMouse()
local Character = player.Character or player.CharacterAdded:wait()

local transformed = Instance.new("BoolValue")
transformed.Name = "Transformed"
transformed.Value = false
transformed.Parent = script

-- body parts

local LUarm = Character:WaitForChild("LeftUpperArm")
local LLarm = Character:WaitForChild("LeftLowerArm")
local Lhand = Character:WaitForChild("LeftHand")
local RUarm = Character:WaitForChild("RightUpperArm")
local RLarm = Character:WaitForChild("RightLowerArm")
local Rhand = Character:WaitForChild("RightHand")
local utorso = Character:WaitForChild("UpperTorso")
local ltorso = Character:WaitForChild("LowerTorso")
local LUleg = Character:WaitForChild("LeftUpperLeg")
local LLleg = Character:WaitForChild("LeftLowerLeg")
local Lfoot = Character:WaitForChild("LeftFoot")
local RUleg = Character:WaitForChild("RightUpperLeg")
local RLleg = Character:WaitForChild("RightLowerLeg")
local Rfoot = Character:WaitForChild("RightFoot")
local head = Character:WaitForChild("Head")
local hroot = Character:WaitForChild("HumanoidRootPart")

local CLUarm = Character.LeftUpperArm:Clone()
local CLLarm = Character.LeftLowerArm:Clone()
local CLhand = Character.LeftHand:Clone()
local CRUarm = Character.RightUpperArm:Clone()
local CRLarm = Character.RightLowerArm:Clone()
local CRhand = Character.RightHand:Clone()
local Cutorso = Character.UpperTorso:Clone()
local Cltorso = Character.LowerTorso:Clone()
local CLUleg = Character.LeftUpperLeg:Clone()
local CLLleg = Character.LeftLowerLeg:Clone()
local CLfoot = Character.LeftFoot:Clone()
local CRUleg = Character.RightUpperLeg:Clone()
local CRLleg = Character.RightLowerLeg:Clone()
local CRfoot = Character.RightFoot:Clone()
local Chead = Character.Head:Clone()
local Chroot = Character.HumanoidRootPart:Clone()
0
i've tried all these answers and it doesn't seem to be a solution to the problem. which then leads me to the question, if the problem is the character loading in, how come these don't work? and if you do have another solution, i'd love to know! jesusbeef 33 — 5y
0
Sorry, change character to Character Thetacah 712 — 5y
0
Sorry, change character to Character Thetacah 712 — 5y
0
let me try again. jesusbeef 33 — 5y
View all comments (10 more)
0
Ok I just added some WaitForChild()'s and edited my code. Should work this time. Thetacah 712 — 5y
0
The WaitForChild() method won't fix the problem. The problem is the character itself being nil along with its children. DeceptiveCaster 3761 — 5y
0
what do you mean? what do you suggest the solution might be? im stuck on this jesusbeef 33 — 5y
0
Where is your script located? DeceptiveCaster 3761 — 5y
0
It's impossible for the Character to be nil using my method, lol. Assuming he's using the R16 rig, my method is 100% functional... I tested it myself. Thetacah 712 — 5y
0
in the starterpack. I am trying to create all these variables and after they all load i am going to make key function that will use these body parts. jesusbeef 33 — 5y
0
Put my code in the StarterPack and it should work.(It will work) Thetacah 712 — 5y
0
Also, why aren't you using the variables you created when cloning lol Thetacah 712 — 5y
0
i just noticed that and im fixing it right now haha, after let me check. jesusbeef 33 — 5y
0
thanks so much! im glad there are people like you who are dedicated to help. appreciate it. thanks man! jesusbeef 33 — 5y
Ad
Log in to vote
1
Answered by 5 years ago

The character hasn't loaded in yet, therefore your attempt at addressing the player's character comes out as nil. To fix this, simply add a wait at the beginning of your script, or better yet utilize the CharacterAdded event to run your code once the character actually loads in.

0
it's a bit odd. the wait function doesn't work, nor does the characteradded. jesusbeef 33 — 5y
Log in to vote
0
Answered by
B_rnz 171
5 years ago

This may seem improper to do, but im going to assume that you have a local script, inside StarterGui..

I would add a wait() before getting Character's body parts.

Hope this works!

Log in to vote
0
Answered by
2ndwann 131
5 years ago
Edited 5 years ago

I think you are using a server-side Script instead of a local script. It will say that when you are trying to access player-specific objects with a server-script. Try using a LocalScript instead, and I hope it will be fine.

If you are already using a LocalScript and it still does not work, try using :Wait() because the character may have not been loaded.

Answer this question