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

Created a local script that is supposed to remove parts when player dies, not working?

So I created a local script that is located inside the Starter Player scripts, and it is supposed to remove the objects, when the player dies.

This clearly works when running in studio, but when i am in-game it malfunctions and the output says

attempt to index field 'Character' (a nil value)

here's the local script.

THE GAME IS FE.


-- // Variables //

local Player = game.Players.LocalPlayer
local repstorage = game.ReplicatedStorage

local part1 = game:FindFirstChild('PART1Clone')
local part2 = game:FindFirstChild('PART2Clone')
local event = repstorage.ServerEvent
local activated = game.ReplicatedStorage.On

-- // Function //

repeat wait() until Player.Character

    Player.Character.Humanoid.Died:connect(function()
	
	part1:Remove()
	part2:Remove()
	
	activated.Value = false

	end)

5 comments

0

line 15 you have no end)?

0

The problem is that the code is running before the player's character has spawned. Try waiting until the character exists, and same with the humanoid.

0

i did that too @thedeadlypanther because the guy that answered told me to do that. look at the answer

0

I fixed your script I beleive.

0

I added it to my answer.

1 answer

When it says

attempt to index field 'Character' (a nil value)

then that means the Character has not yet loaded into the game. Thus meaning you need to wait for the character to load. Now there are many different ways of doing this. One way is to set a repetition until the player loads.

repeat wait() until Player.Character

this waits until the player is present before continuing through the script. This is one of the shortest methods of doing so. Another way is to set a CharacterAdded function

Player.CharacterAdded:connect(function()

end

this method enables you to do something to the character at the moment of it loading.

I hope this helped you and if not feel free to comment and I will assist you further.

Here is a script you may try

-- // Variables //
 
local Player = game.Players.LocalPlayer
local repstorage = game.ReplicatedStorage
local Char = Player.Character or Player.CharacterAdded:Wait()
local part1 = game:FindFirstChild('PART1Clone')
local part2 = game:FindFirstChild('PART2Clone')
local event = repstorage.ServerEvent
local activated = game.ReplicatedStorage.On
 
-- // Function //

repeat wait() until Char.Humanoid
Player.Character.Humanoid.Died:connect(function()
	part1:Remove()
	part2:Remove()
 
	activated.Value = false
 
end)

7 comments

0

it doesnt work, my game is FE, should i add like a remote event or remote function ?

0

it also says > Humanoid is not a part of Model

0

@Amputea: After the "repeat wait() until Player.Character" try "Player.Character:WaitForChild("Humanoid").Died" ?

0

ok

0

no, it doesn't work

0

i did it. it says > attempt to index upvalue 'part1' (a nil value)

0

Ok so what you do then is instead of putting wait() for character use Character.Humanoid

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