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