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

Why isn't the game recognizing if the player is alive or not?

Asked by
duffus 0
4 years ago

So I am making a sort of race script wherein a player touches a start brick to start a timer for himself and then once he reaches the end brick. I got it to work, but then I decided to improve upon it by making the player unable to start unless they are alive and if they die during the race, their race ends.

When I test it, everything works fine until after the player dies and the script seems to break. It seems to think that the player is still dead when it reaches the if statements in the endRace function despite me verifying with print statements that the player is, in fact, alive. Whenever I reach the end brick after dying and starting the race again, if gives me the message in the output "[player name] has died!" (you can find where this is in the endRace function).

As a side note, I realize that instead of using the alive variable and the two functions alive alive() and died() to check if they are alive, I can check the humanoid's health, but the exact same result occurs. This is method is simply my latest iteration in trying to somehow fix this problem. Furthermore, for the sake of saving space, I will simply tell you instead, helpful answer-er, that the start and finish bricks are the ones containing the code that fire the remote events for the startRace and endRace functions, and that they work fine. My problems are contained within this block of code.

If anyone could help me with this problem of the game not recognizing that the player is alive I would greatly appreciate it!

local player = game.Players.LocalPlayer
local startEvent = game.ReplicatedStorage.StartRace
local endEvent = game.ReplicatedStorage.EndRace
local started = false
local raceTime = nil
local bestTime = nil
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local alive = true

-- Start player timer
local function startRace()
    if not started and alive then
        print(player.Name.." is now racing")
        started = true
        startTime = tick()
    end     
end


-- End player timer
local function endRace()
    if started then
        if (alive) then
            print(player.Name.." has stopped racing")
            started = false
            local endTime = tick()

            -- Calculate and display time
            raceTime = endTime - startTime

            if bestTime then
                if raceTime<bestTime then
                    bestTime = raceTime
                end
            else
                bestTime = raceTime 
            end
            player.PlayerGui.LastTimeGui.TimeLabel.Text = "Last time: "..raceTime
            player.PlayerGui.BestTimeGui.TimeLabel.Text = "Best time: "..bestTime

        elseif (not alive) then
            print(player.Name.." has died!")
            started = false
        end
    end 
end

local function died()
    alive = false
    print("the died function has run and alive is")
    print(alive)    
end

local function alive()
    alive = true
    print("the alive function has run and alive is ")
    print(alive)
end



-- Events
player.CharacterAdded:Connect(alive)
humanoid.Died:Connect(died)


startEvent.OnClientEvent:Connect(startRace)
endEvent.OnClientEvent:Connect(endRace)
humanoid.Died:Connect(endRace)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Well I don't know what monstrosity you have done there, But I see the title and I'm only gonna answer that, Here is what I wrote

local plr = game:GetService("Player").LocalPlayer
local IsAlive = true

if plr.Humanoid then
    if plr.Humanoid.Health == 0 then
        IsAlive = false
        print("THIS HUMAN HAS DIED CALL FOR HELP")
end
end

NOT TESTED but should work

EDIT if that dose not work use this

local IsAlive = true

game:GetService('Players').PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            print("THIS HUMAN HAS DIED CALL FOR HELP")
\       IsAlive = false
        end)
    end)
end)
Ad

Answer this question