Hi! I’m creating an Obby and I have a question, how could I create a script that upon entering makes the player die, but only once, that is, go in and the script kills it and then the player continues normally. I hope someone can help me thank you very much
Maybe something like this? I’m not sure what you’re trying to accomplish, but maybe you can find a way to implement this into your script somehow:
local killed = false
-- in some event
if not killed then
player.Character.Humanoid.Health = 0
killed = true
end
If you can elaborate what you’re trying to accomplish, I can give you more useful code examples.
So when a player enters the game, the script automatically kills him, the player does not need to touch any block, the idea is that he enters and the script kills him but only once, that is, it kills him and the player revives normally and can continue playing , I know it makes no sense to ask for this script but I need the player to die as soon as you enter, also, where would the script go? In which folder.
Anyway the script does not work
Maybe try this (ServerScript
inside of ServerScriptService
):
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
humanoid.Health = 0
end)
It did not work, when I entered the game my character had 0 life but was still alive and life rose slowly and the idea is that when you enter you die and then you reappear normal and continue playing …
You wouldn’t use CharacterAdded
, since it would fire when they respawn as well.
You can use player.Character, but you have to make sure character isn’t nil, otherwise Humanoid, health would error.
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
repeat
wait()
until player.Character ~= nil
player.Character.Humanoid.Health = 0
end)
Hey! This worked, thank you very much
What does it mean that the character is not null? I was left with the doubt
nil means it doesn’t exist, so the code will repeat wait() until the character exists (does not equal nil)