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

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

Asked by 6 years ago

So I have this script and it is supposed to off set the camera "in other words to shake the screen". Here is the error message:

Players.Player2.PlayerGui.LocalScript:2: attempt to index field 'Character' (a nil value)

Here is the script:

***for i = math.huge, math.huge do
    game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(math.random(-2,2),math.random(-2,2),math.random(-2,2))
    wait()
end
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(0,0,0)***

I'm in need of help! :D

0
Oh by the way, the *'s are not in the script. TheLionLiar 39 — 6y
0
You probably need to wait for the character to load, as that's most common problem. :/ (Script loads before the character loads.) TheeDeathCaster 2368 — 6y
0
My suggestion, try game.Players.LocalPlayer.CharacterAdded:Wait() If you set any variables to equal this, it will be set to the Character model once the event has executed. M39a9am3R 3210 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago

@ line 2, put game:GetService("Players").LocalPlayer.Character.Humanoid.CameraOffset also, if it says character, Humanoid, or CameraOffset doesnt exist, place a WaitForChild()

0
The issue is the character not the Players your solution for GetService would of been a probable fix if it was erroring about Players. GetGlobals 343 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

The problem is, that a script runs before the character even gets added. So the Character doesn't exist by the time you are running the script. An efficient way to prevent it is to wait until the character loads using game.Players.LocalPlayer.CharacterAdded:Wait().

This is how the code will look:

game.Players.LocalPlayer.CharacterAdded:Wait()

for i = math.huge, math.huge do
    game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(math.random(-2,2),math.random(-2,2),math.random(-2,2))
    wait()
end
game.Players.LocalPlayer.Character.Humanoid.CameraOffset = Vector3.new(0,0,0)

Answer this question