Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does line 5 Keep erroring out?

For some reason it keeps erroring out and giving me this message

Players.YouShalllNotPass.Backpack.FuncDeath:5 : Attempt to index field 'Character' (a nil Value)

IT IS A LOCALSCRIPT

here's my script :

local character = game.Players.LocalPlayer.Character
local player = game.Players.LocalPlayer
local char = game.Players.LocalPlayer.Character

game.Players.LocalPlayer.Character.Humanoid.Changed:connect(function(death)
if game.Players.LocalPlayer.Character.Humanoid.Health == 0 then
local i = game.ServerStorage.Death:Clone()
local o = game.ServerStorage.Death:Clone()
local p = game.ServerStorage.Death:Clone()
local a = game.ServerStorage.Death:Clone()
local s = game.ServerStorage.Death:Clone()
local d = game.ServerStorage.Death:Clone()
i.Parent = character.Torso
o.Parent = character:findFirstChild("Right Arm")
p.Parent = character:findFirstChild("Right Leg")
a.Parent = character:FindFirstChild("Left Arm")
s.Parent = character:FindFirstChild("Left Leg")
-- Limbs Disappear
character:findFirstChild("Right Arm").Transparency = 1
character:findFirstChild("Right Leg").Transparency = 1
character:FindFirstChild("Left Arm").Transparency = 1
character:FindFirstChild("Left Leg").Transparency = 1
-- Clothing Disappear
	if char:FindFirstChild("Pants") then
    char.Pants:Destroy()
	if char:FindFirstChild("Shirt") then
    char.Shirt:Destroy()
	if char:FindFirstChild("Hat") then
    char.Hat:Destroy()
	if char:FindFirstChild("Hat") then
    char.Hat:Destroy()
	if char:FindFirstChild("Hat") then
    char.Hat:Destroy()
						end
					end
				end
			end
		end
	end
end)

3 answers

"LocalPlayer" is always nil to a server script. Is this a Script or a LocalScript? If it's a Script, you need to find some other way to get a reference to the player object.

EDIT: So the problem is that the Character has not spawned yet. This code checks waits for the character to spawn. If the character's parent is nil, it means that the LocalScript ran before the old character was removed, so you need to wait for the new character to spawn.

local player = game.Players.LocalPlayer

local character = Player.Character
if not character or character.Parent == nil then
    character = player.CharacterAdded:wait()
end

--now the character exists

2 comments

0

local

0

LocalScripts often run before the Character spawns. On line 5 you're assuming that the character exists and has a humanoid. I edited the answer to include how to fix this.

The hidden variable LocalPLayer, can only be accessed on the *Client * therefore LocalPlayer can accessed only be accessed on the Client! To access what the Client sees, Use the code in a Local Script!

I see you have to access the ServerStorage which can only be accessed by Scripts or Server-Sided Scripts. You may have to go to the Roblox Wiki and manually search! I'll try to hep you though!

References: LocalPlayer = Player in the Client Side Client = Your computer Local Script = Scripts that only run and affect client-sided objects on Client Server Storage = A storage system that is useful for maps(Replacement for using Lighting as Storage Server-Sided Script = A script that runs on the server and cannot be accessed by Client

2 comments

0

Thanks for telling me nothing I already didn't know now if you would of read my question better you probably could of helped me

0

Sorry, I tried to help!

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