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

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

Asked by 7 years ago
Edited 7 years ago

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.

01-- // Variables //
02 
03local Player = game.Players.LocalPlayer
04local repstorage = game.ReplicatedStorage
05 
06local part1 = game:FindFirstChild('PART1Clone')
07local part2 = game:FindFirstChild('PART2Clone')
08local event = repstorage.ServerEvent
09local activated = game.ReplicatedStorage.On
10 
11-- // Function //
12 
13repeat wait() until Player.Character
14 
15    Player.Character.Humanoid.Died:connect(function()
View all 22 lines...
0
line 15 you have no end)? LifeInDevelopment 364 — 7y
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. TheDeadlyPanther 2460 — 7y
0
i did that too @thedeadlypanther because the guy that answered told me to do that. look at the answer Amputea 2 — 7y
0
I fixed your script I beleive. LastApollo 76 — 7y
0
I added it to my answer. LastApollo 76 — 7y

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

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.

1repeat 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

1Player.CharacterAdded:connect(function()
2 
3end

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

01-- // Variables //
02 
03local Player = game.Players.LocalPlayer
04local repstorage = game.ReplicatedStorage
05local Char = Player.Character or Player.CharacterAdded:Wait()
06local part1 = game:FindFirstChild('PART1Clone')
07local part2 = game:FindFirstChild('PART2Clone')
08local event = repstorage.ServerEvent
09local activated = game.ReplicatedStorage.On
10 
11-- // Function //
12 
13repeat wait() until Char.Humanoid
14Player.Character.Humanoid.Died:connect(function()
15    part1:Remove()
16    part2:Remove()
17 
18    activated.Value = false
19 
20end)
0
it doesnt work, my game is FE, should i add like a remote event or remote function ? Amputea 2 — 7y
0
it also says > Humanoid is not a part of Model Amputea 2 — 7y
0
@Amputea: After the "repeat wait() until Player.Character" try "Player.Character:WaitForChild("Humanoid").Died" ? chess123mate 5873 — 7y
0
ok Amputea 2 — 7y
View all comments (3 more)
0
no, it doesn't work Amputea 2 — 7y
0
i did it. it says > attempt to index upvalue 'part1' (a nil value) Amputea 2 — 7y
0
Ok so what you do then is instead of putting wait() for character use Character.Humanoid LastApollo 76 — 7y
Ad

Answer this question