Humanoid is not being re-defined

I want to make a script where if you are below a certain health, you will become in a “downed” state and have blur and a ColorCorrectionEffect applied to you.

I want to be able for this script to run even after character has respawned, but once the character respawns, all the functions that require the humanoid break even though I’ve re-defined the new humanoid and character inside my respawn function, any reasons why?

Script Location - StarterCharacterScripts

This has been bugging me for the past 2 days, any help would be appreciated!

wait()
-- Variables (Services)
local Lighting = game.Lighting
local ContentProvider = game:GetService("ContentProvider")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

-- Variables (Player, PlayerModules)
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Controls = require(plr.PlayerScripts.PlayerModule):GetControls()

-- Variables (Instances, Folders)
local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui") 
local Sounds = game:GetService("ReplicatedStorage"):WaitForChild("Sounds")

-- Variables (Booleans)
local debounce = false

-- Dictionaries (Customizable)
local customizable = {
    ["LowHPHealth"] = 30; -- Minimum HP until effects apply.
    ["DownedHealth"] = 10; -- What health should the player be downed.
    ["GetUpHealth"] = 20; -- What health should the player be able to get up.
    ["Intensity"] = 10 -- Lower is darker, higher is lighter.
}

local function preloadAssets(assets)
    -- Preloads assets inside an array.
    ContentProvider:PreloadAsync(assets)
    print("Preloaded assets")
end

-- Preloads all the sounds needed
preloadAssets(Sounds:GetChildren())

humanoid.HealthChanged:Connect(function(health)
    print(char.Parent)
    -- Variables (Instances, Integers)
    local BlurEffect = Lighting.BlurLowHP
    local ColourOverlay = Lighting.LowHPColourOverlay
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local newcolor = 170/health
    
    -- Variables (Tweens)
    local blurTween = TweenService:Create(BlurEffect, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {Size = math.clamp(customizable.LowHPHealth - humanoid.Health, 0, 22)})
    local tintTween = TweenService:Create(ColourOverlay, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {TintColor = Color3.new(math.clamp(1 - ((newcolor * customizable.Intensity) / 450), 0, 1), 0, 0)})
    local tintTweenEnd = TweenService:Create(ColourOverlay, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {TintColor =  Color3.fromRGB(255, 255, 255)})

    if health <= customizable.LowHPHealth then
        -- Applies all the "LowHP" effects onto player.
        BlurEffect.Enabled = true
        ColourOverlay.Enabled = true
        tintTween:Play()
        blurTween:Play()

        if not heartbeat.Playing then
            heartbeat.Playing = true
        end
        heartbeat.Volume = (customizable.LowHPHealth - humanoid.Health)/5 
        -- Heartbeat becomes louder the lower the health.

        if health <= customizable.DownedHealth then
            Controls:Disable()
            humanoid.Sit = true

        elseif health >= customizable.GetUpHealth then
            Controls:Enable()
            humanoid.Sit = false
        end
        
    else
        -- If the health remains above 30, it won't apply the effects.
        heartbeat.Volume = 0
        BlurEffect.Size = 0
        BlurEffect.Enabled = false

        tintTweenEnd:Play()
        tintTweenEnd.Completed:Wait()

        ColourOverlay.Enabled = false
        heartbeat:Stop()
    end
end)

humanoid.Died:Connect(function()
    -- Variables (Instances)
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local flatline = Sounds.FlatlineDeath
    
    heartbeat:Stop()
    flatline:Play()
end)

plr.CharacterAdded:Connect(function(character)
    -- Variables (Instances)
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local flatline = Sounds.FlatlineDeath
    local BlurEffect = Lighting.BlurLowHP
    local ColourOverlay = Lighting.LowHPColourOverlay
    
    -- Re-define player variables.
    char = character
    humanoid = char:WaitForChild("Humanoid", 25)
    
    -- Disable all the effects.
    Controls:Enable()
    flatline:Stop()
    BlurEffect.Size = 0

    BlurEffect.Enabled = false
    ColourOverlay.Enabled = false
end)

Thanks!

Please try putting this in the StarterCharacterScripts. I think that should solve it. :grinning_face_with_smiling_eyes:

I spent 7 hours trying to solve the first issue, and found out that the issue was StarterCharacterScripts as

plr.CharacterAdded:Connect(function(character)

doesn’t run if put in StarterCharacterScripts because everytime a character loads, it refreshes all the scripts inside, which means that the plr.CharacterAdded function doesn’t have any time to run as it’s being reloaded.

Okay, I can understand your confusion. When you make a connection, redefining the variable/instance does not redefine the connection. You will have to redefine the connection every time the character respawns.

wait()
-- Variables (Services)
local Lighting = game.Lighting
local ContentProvider = game:GetService("ContentProvider")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

-- Variables (Player, PlayerModules)
local plr = Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local Connection1,Connection2
local Controls = require(plr.PlayerScripts.PlayerModule):GetControls()

-- Variables (Instances, Folders)
local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui") 
local Sounds = game:GetService("ReplicatedStorage"):WaitForChild("Sounds")

-- Variables (Booleans)
local debounce = false

-- Dictionaries (Customizable)
local customizable = {
    ["LowHPHealth"] = 30; -- Minimum HP until effects apply.
    ["DownedHealth"] = 10; -- What health should the player be downed.
    ["GetUpHealth"] = 20; -- What health should the player be able to get up.
    ["Intensity"] = 10 -- Lower is darker, higher is lighter.
}

local function preloadAssets(assets)
    -- Preloads assets inside an array.
    ContentProvider:PreloadAsync(assets)
    print("Preloaded assets")
end

-- Preloads all the sounds needed
preloadAssets(Sounds:GetChildren())

function CreateConnection()
if Connection1 then Connection1:Disconnect() end
if Connection2 then Connection2:Disconnect() end
Connection1 = humanoid.HealthChanged:Connect(function(health)
    print(char.Parent)
    -- Variables (Instances, Integers)
    local BlurEffect = Lighting.BlurLowHP
    local ColourOverlay = Lighting.LowHPColourOverlay
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local newcolor = 170/health
    
    -- Variables (Tweens)
    local blurTween = TweenService:Create(BlurEffect, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {Size = math.clamp(customizable.LowHPHealth - humanoid.Health, 0, 22)})
    local tintTween = TweenService:Create(ColourOverlay, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {TintColor = Color3.new(math.clamp(1 - ((newcolor * customizable.Intensity) / 450), 0, 1), 0, 0)})
    local tintTweenEnd = TweenService:Create(ColourOverlay, TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut, 0, false, 0), {TintColor =  Color3.fromRGB(255, 255, 255)})

    if health <= customizable.LowHPHealth then
        -- Applies all the "LowHP" effects onto player.
        BlurEffect.Enabled = true
        ColourOverlay.Enabled = true
        tintTween:Play()
        blurTween:Play()

        if not heartbeat.Playing then
            heartbeat.Playing = true
        end
        heartbeat.Volume = (customizable.LowHPHealth - humanoid.Health)/5 
        -- Heartbeat becomes louder the lower the health.

        if health <= customizable.DownedHealth then
            Controls:Disable()
            humanoid.Sit = true

        elseif health >= customizable.GetUpHealth then
            Controls:Enable()
            humanoid.Sit = false
        end
        
    else
        -- If the health remains above 30, it won't apply the effects.
        heartbeat.Volume = 0
        BlurEffect.Size = 0
        BlurEffect.Enabled = false

        tintTweenEnd:Play()
        tintTweenEnd.Completed:Wait()

        ColourOverlay.Enabled = false
        heartbeat:Stop()
    end
end)

Connection2 = humanoid.Died:Connect(function()
    -- Variables (Instances)
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local flatline = Sounds.FlatlineDeath
    
    heartbeat:Stop()
    flatline:Play()
end)
end

CreateConnection()

plr.CharacterAdded:Connect(function(character)
    -- Variables (Instances)
    local heartbeat = Sounds.LowHealthHeartbeatSound
    local flatline = Sounds.FlatlineDeath
    local BlurEffect = Lighting.BlurLowHP
    local ColourOverlay = Lighting.LowHPColourOverlay
    
    -- Re-define player variables.
    char = character
    humanoid = char:WaitForChild("Humanoid", 25)
    CreateConnection()

    -- Disable all the effects.
    Controls:Enable()
    flatline:Stop()
    BlurEffect.Size = 0

    BlurEffect.Enabled = false
    ColourOverlay.Enabled = false
end)

Hope this helps! :grinning_face_with_smiling_eyes:

Thank you so much for your help! This works completely and I understand it more! :slight_smile:

So the problem was that Humanoid connections were still linked to the old humanoid? And your new script refreshes them to make sure that they are listening to the new Humanoid?

1 Like