This script works fine except for the while true do loop. It’ll make the player blink at the correct time once, and then decides to change between the normal and blink face constantly for no reason. I added some print() lines and it does seem to be looping properly, but it simply decides to ignore the task.wait(7.25) line for some reason. Any help or insight on this strange problem would be appreciated lol.
local hum = script.Parent.Humanoid
local oldHealth = hum.Health
hum.HealthChanged:Connect(function(newHealth)
if oldHealth > newHealth then
oldHealth = newHealth
local texture1 = "http://www.roblox.com/asset/?id=15274295813"
script.Parent.Head.face.Texture = texture1
task.wait(1)
elseif newHealth >= 70 then
while true do
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=7279789528"
task.wait(7.25)
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=461335489"
task.wait(0.5)
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=7279789528"
end
elseif newHealth >= 40 then
local texture1 = "http://www.roblox.com/asset/?id=9079806307"
script.Parent.Head.face.Texture = texture1
task.wait(1)
elseif newHealth <= 0 then
local texture2 = "http://www.roblox.com/asset/?id=4820464519"
script.Parent.Head.face.Texture = texture2
task.wait(1)
end
end)
Might try this. I don’t know if this will work, but I hope it does.
local states = {
Damaged = 0,
Blinking = 1,
Below70 = 2,
Dead = 3
}
local faces = {
damaged = {
texture = "http://www.roblox.com/asset/?id=15274295813",
delayBy = 1,
},
blinking = {
{
texture = "http://www.roblox.com/asset/?id=7279789528",
delayBy = 7.25
},
{
texture = "http://www.roblox.com/asset/?id=461335489",
delayBy = 0.5
},
},
below70 = {
texture = "http://www.roblox.com/asset/?id=9079806307",
delayBy = 1,
},
dead = {
texture = "http://www.roblox.com/asset/?id=4820464519",
delayBy = 1,
}
}
local hum = script.Parent.Humanoid
local oldHealth, healthState = hum.Health, (if hum.Health >= 70 then states.Blinking else if hum.health >= 40 then states.Below70 else if hum.Health <= 0 then states.Dead else -1)
local RunLoop, animStep, last, delayBy = nil,1,os.clock(), 1
hum.HealthChanged:Connect(function(newHealth)
if oldHealth > newHealth then
oldHealth = newHealth
healthState = states.Damaged
elseif newHealth >= 70 then
healthState = states.Blinking
elseif newHealth >= 40 then
healthState = states.Below70
elseif newHealth <= 0 then
healthState = states.Dead
end
end)
RunLoop = game:GetService("RunService").PreRender:Connect(function(delta)
local switch = healthState
local current = os.clock()
if switch == states.Damaged and (current >= (last + delayBy)) then
local texture1 = faces.damaged.texture
last = current
delayBy = faces.damaged.delayBy
script.Parent.Head.face.Texture = texture1
end
if switch == states.Blinking then
if current >= (last + delayBy) then
local FaceData = faces.blinking[animStep]
if FaceData then
local TimeDelay = FaceData.delayBy
local texture1 = FaceData.texture
last = current
delayBy = TimeDelay
script.Parent.Head.face.Texture = texture1
animStep += 1;
end
if animStep > #faces.blinking then
animStep = 1
end
end
end
if switch == states.Below70 and (current >= (last + delayBy)) then
local texture1 = faces.below70.texture
last = current
delayBy = faces.below70.delayBy
script.Parent.Head.face.Texture = texture1
end
if switch == states.Dead and (current >= (last + delayBy)) then
local texture1 = faces.dead.texture
last = current
delayBy = faces.dead.delayBy
script.Parent.Head.face.Texture = texture1
end
end)
First and foremost, you are creating a memory leak. Each time you take damage and that newHealth is over or equal to 70, you are creating a new while loop.
And the problem above is the root cause of this post.
Multiple while loops are running at the same time, so you actually have multiple loops telling you to blink or not. This will overlap and make your script go crazy.
To fix this?
Bring the while loop outside, and use a variable to activate the while loop or not.
local shouldBlink: bool = true
-- your stuff here
hum.HealthChanged:Connect(function(newHealth)
-- stuff here
end
while true do
if shouldBlink == false then continue end
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=7279789528"
task.wait(7.25)
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=461335489"
task.wait(0.5)
script.Parent.Head.face.Texture = "http://www.roblox.com/asset/?id=7279789528"
end
When you want to stop the blinking, just set shouldBlink to true, on the contrary, set it to false
I didn’t bother providing you with a new script because your formatting is kind of weird and your code is hard to read.