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 'Character' (a nil value)?

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

3 comments

0

Oh by the way, the *'s are not in the script.

0

You probably need to wait for the character to load, as that's most common problem. :/ (Script loads before the character loads.)

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.

2 answers

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

1 comment

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.

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)

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