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

Why does line 5 Keep erroring out?

Asked by
Prioxis 673 Moderation Voter
10 years ago

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

Log in to vote
2
Answered by
Merely 2122 Moderation Voter Community Moderator
10 years ago

"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
0
local Prioxis 673 — 10y
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. Merely 2122 — 10y
Ad
Log in to vote
2
Answered by
W8X 95
10 years ago

You can't access ServerStorage from a LocalScript, Put the objects in ReplicatedStorage instead.

http://wiki.roblox.com/index.php?title=ReplicatedStorage

http://wiki.roblox.com/index.php?title=ServerStorage

Log in to vote
0
Answered by
Emg14 10
10 years ago

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

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 Prioxis 673 — 10y
0
Sorry, I tried to help! Emg14 10 — 10y

Answer this question