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.

01-- transform script
02 
03local player = game.Players.LocalPlayer
04local mouse = script.Parent.Parent:GetMouse()
05local Character = player.Character
06 
07local transformed = Instance.new("BoolValue")
08transformed.Name = "Transformed"
09transformed.Value = false
10transformed.Parent = script
11 
12-- body parts
13 
14local LUarm = Character.LeftUpperArm
15local LLarm = Character.LeftLowerArm
View all 46 lines...
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:

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

Full code:

01-- transform script
02 
03-- transform script
04 
05local player = game.Players.LocalPlayer
06local mouse = script.Parent.Parent:GetMouse()
07local Character = player.Character or player.CharacterAdded:wait()
08 
09local transformed = Instance.new("BoolValue")
10transformed.Name = "Transformed"
11transformed.Value = false
12transformed.Parent = script
13 
14-- body parts
15 
View all 48 lines...
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