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.


-- // 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)
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.

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)
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