I’m trying to detect when the player falls into the void, I assume that when the Player or any other instance falls into the void it gets destroyed? I used Humanoid Health although It didn’t work is there another way to do that?
-- Variables
local replicatedService = game:GetService("ReplicatedStorage")
local Character = workspace:FindFirstChild(game.Players.LocalPlayer.Name)
-- Code (playerDeath)
repeat wait() until game:GetService("Players").LocalPlayer
if game.Players.LocalPlayer.Character.Humanoid.Health <= 0 then
replicatedService.minigameEvents.playerDeath:FireServer(script.Parent.playerDeath.Value)
print("Player:" , game.Players.LocalPlayer.Name , "Has Died..")
end
There’s a few performance improvements and changes that you could’ve did
local player = game:GetService("Players").LocalPlayer -- when a local script loads, the localplayer has already loaded, you don't need to loop to check for it
local character = player.Character or player.CharacterAdded:Wait() -- get the character or wait for it to load
local humanoid = character:WaitForChild("Humanoid") -- get the humanoid
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("minigameEvents").playerDeath
humanoid.Died:Connect(function() -- detect when the player died
remoteEvent:FireServer(script.Parent.playerDeath.Value)
end)
You could do this to update the script whenever the player dies (depending on where it’s placed)
If this is a local script in starter character scripts, you can keep the previous script, but if it’s placed anywhere else, consider the following:
local player = game:GetService("Players").LocalPlayer -- when a local script loads, the localplayer has already loaded, you don't need to loop to check for it
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = replicatedStorage:WaitForChild("minigameEvents").playerDeath
local character -- this is an upvalue, this is essentially "local character = nil"
local humanoid
local function CreatePlayerData(char) -- this is a function to create updated upvalues in the script
character = char -- set the character upvalue to the "char" parameter
humanoid = character:WaitForChild("Humanoid") -- set the humanoid variable to the new humanoid
humanoid.Died:Connect(function()
remoteEvent:FireServer(script.Parent.playerDeath.Value)
end)
end
player.CharacterAdded:Connect(CreatePlayerData) -- whenever the player respawns, this function is called to reset the script
The character model is not destroyed when a player falls into the void, only the parts that are children of the character will. The Humanoid will enter the Dead state though, so you can always check for that. The reason why your code originally didn’t work is because your check only runs immediately after LocalPlayer is non-nil.