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.