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