Script isn't Responsive

Hey there!
I’m currently trying to make a shield system for future projects, currently the script does not detect when the function; “Humanoid.HealthChanged()”

I’ve tried placing the script inside the Character and inside “ServerScriptService”.

Code (Server-Script, ServerScriptService)

local players = game.Players

players.PlayerAdded:Connect(function(player)
	
	
	player.CharacterAdded:Connect(function(model)
		
		if model:FindFirstChild("Humanoid") then
			model.Humanoid:SetAttribute('Shield', 100)
		end
		
		local character = model
		local humanoid = character.Humanoid
		print('hi?')
		
		if humanoid then
			print('hi')
		end

		local lastHealth = humanoid.Health

		humanoid.HealthChanged:Connect(function()
			print('yeh')
			if lastHealth > humanoid.Health then -- took damage
				local diff = lastHealth-humanoid.Health
				print('mhm')
				if humanoid:GetAttribute('Shield') then
					print('found it')
					
					if humanoid:GetAttribute('Shield') > diff then
						
						humanoid.Health = lastHealth
						humanoid:SetAttribute('Shield', humanoid:GetAttribute('Shield')-diff)
						print('answer1')
						lastHealth = humanoid.Health
						
					elseif humanoid:GetAttribute('Shield') == diff then
						
						humanoid.Health = lastHealth
						humanoid:SetAttribute('Shield', 0)
						print('answer2')
						lastHealth = humanoid.Health
						
					elseif humanoid:GetAttribute('Shield') < diff and humanoid:GetAttribute('Shield') ~= 0 then
						
						local inverseDiff = diff-humanoid:GetAttribute('Shield')
						humanoid:SetAttribute('Shield', 0)
						humanoid.Health = lastHealth-inverseDiff
						print('answer3')
						lastHealth = humanoid.Health
					end
				end
			else
			end
		end)
	end)
end)

I just need to know why it isn’t working, and how i can avoid this problem in future.

2 Likes

Hello Catstronomical, please try:

humanoid.Health.Changed

I know it’s technically the same, but you never know with Roblox. See if that works and report back to cheer my name or show the debugger.

Nik

you know that the Health object is a direct value given, thats why there’s a HealthChanged function.

The script works fine locally for some reason.

local players = game.Players

players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")

        if humanoid and humanoid.Health > 0 then
            humanoid:SetAttribute('Shield', 100)

            local lastHealth = humanoid.Health

            humanoid.HealthChanged:Connect(function()
                if lastHealth > humanoid.Health then -- took damage
                    local diff = lastHealth - humanoid.Health

                    if humanoid:GetAttribute('Shield') then
                        if humanoid:GetAttribute('Shield') > diff then
                            humanoid.Health = lastHealth
                            humanoid:SetAttribute('Shield', humanoid:GetAttribute('Shield') - diff)
                            lastHealth = humanoid.Health
                        elseif humanoid:GetAttribute('Shield') == diff then
                            humanoid.Health = lastHealth
                            humanoid:SetAttribute('Shield', 0)
                            lastHealth = humanoid.Health
                        elseif humanoid:GetAttribute('Shield') < diff and humanoid:GetAttribute('Shield') ~= 0 then
                            local inverseDiff = diff - humanoid:GetAttribute('Shield')
                            humanoid:SetAttribute('Shield', 0)
                            humanoid.Health = lastHealth - inverseDiff
                            lastHealth = humanoid.Health
                        end
                    end
                else
                    -- Handle health increase if needed
                end
            end)
        end
    end)
end)

would you try it this way, I think it’s improved

Same thing.

Just had to hook it up to some remote events and had to change the player’s health through the script works fine just need to fix the problems now

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.