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

Need help on line 19 ?

everytime I run my script I get this in the output

It keeps saying the variables so character and player are nil values... and its really starting to piss me off because with every single variable I create it says the variable is nil and the entire reason why the script isn't working is because of it

edited script below

local player = game:GetService("Players").LocalPlayer
game.Players.LocalPlayer.CharacterAdded:connect(function(Char)
local character = game.Players.LocalPlayer.Character

repeat
    wait()
until player.Character
character:WaitForChild("Torso")
character:WaitForChild("Head")
character:WaitForChild("Right Leg")
character:WaitForChild("Right Arm")
character:WaitForChild("Left Arm")
character:WaitForChild("Left Leg")
character:WaitForChild("Humanoid")

end)


character.Humanoid.Changed:connect(function(death)
	
if character.Humanoid.Health == 0 then
local i = game.Lighting.Death:Clone()
local o = game.Lighting.Death:Clone()
local p = game.Lighting.Death:Clone()
local a = game.Lighting.Death:Clone()
local s = game.Lighting.Death:Clone()
local d = game.Lighting.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")
d.Parent = character:FindFirstChild("Head")
-- 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
character:FindFirstChild("Head").Transparency = 1
character:FindFirstChild("Torso").Transparnecy= 1 
-- Clothing Disappear
	if character:FindFirstChild("Pants") then
    character.Pants:Destroy()
	if character:FindFirstChild("Shirt") then
    character.Shirt:Destroy()
	if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
	if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
	if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
						end
					end
				end
			end
		end
	end
end)

2 answers

✓ Accepted answer

The problem is is that the character isn't loaded when the script executes, thus there is no char. What I suggest is to put this at the top of the script:

game.Players.LocalPlayer.CharacterAdded:connect(function(character)

that way, it will fire right when the character loads. This also can save some confusion because the variable passed into the function is the character that just spawned, so you can refer to that as your character.

Also, if there are some things under the character that are throwing errors, try using a character:WaitForChild("What you need") on the character so that it will guarantee that the object will be there.

Here is an example of how you can use it:

game.Players.LocalPlayer.CharacterAdded:connect(function(character)


---------None of this is really needed anymore because of this---------------

--local character = game.Players.LocalPlayer.character
--local character = game.Players.LocalPlayer.character
--local player = game:GetService("Players").LocalPlayer
--local found = game.Workspace:findFirstChild(player.Name)
--if found then
--print'character has loaded'
--else
--while not player.character do wait() end
--end



-----------THe Humanaoid might load after everything else as well
local human = character:FindFirstChild("Humanoid")

human.Changed:connect(function(death) -- error
if human.Health == 0 then -- error
local i = game.Lighting.Death:Clone()
local o = game.Lighting.Death:Clone()
local p = game.Lighting.Death:Clone()
local a = game.Lighting.Death:Clone()
local s = game.Lighting.Death:Clone()
local d = game.Lighting.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
character:FindFirstChild("Head").Transparency = 1
character:FindFirstChild("Torso").Transparnecy= 1 
-- Clothing Disappear
    if character:FindFirstChild("Pants") then
    character.Pants:Destroy()
    if character:FindFirstChild("Shirt") then
    character.Shirt:Destroy()
    if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
    if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
    if character:FindFirstChild("Hat") then
    character.Hat:Destroy()
                        end
                    end
                end
            end
        end
    end
end)

end)

5 comments

0

As I have pointed out numerous times, WaitForChild("Character") does NOT work because the Character is NOT a child of the Player.

0

Thanks for the head's up. I just edited it so that it is more clear.

0

I would of appreciated if you would of atleast showed a script of how the beginning of mine should look to give me some idea on what you actually mean

0

Thank you for the example but sadly it doesn't work either....

0

Sorry, Sorry, I'm new here. I just put it in. Also, with your edited script, it is because character is local to the CharacterAdded function. You need to encase the entire code into the CharacterAdded function for it to work.

At the moment when LocalScripts start executing, the Character hasn't yet loaded, so the property will be nil.

That is, you will be assigning nil to char in line 2.

Waiting until the Character loads should be done first. You can do that something like this:

repeat
	wait()
until player.Character

(I would suggest moving the definition of player to the top and defining char / character (You have unnecessarily two) to actually use the definition)


Your indentation is really bad. It should be meaningful, and actually reflect the grouping of statements. It makes code much easier to read.

Especially since the code you have doesn't work they way you would like it to, and indentation would reveal that.

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