Basically, the HealthChanged event doesn’t fire as quickly as I want it to run, which causes inaccurate damage indicators.
(accurate event (WHAT I WANT)) (this works randomly)
(nonaccurate event (THE THING IM STUCK AT))
(FYI, this doesn’t just work after the first time i shoot, this can also happen the first time i shoot)
(sorry i used a lot of warns, it stands out more than print)
On the accurate event, the warn("TAKEN DAMAGE")
appears in the output around the 250 mark.
However, on the nonaccurate damage, it fires warn("TAKEN DAMAGE")
appears far less than the 250 mark. (and no, hitting the other dummies did NOT affect the event)
Also, FYI, each NPC has their own, unique server script with the exact code that has the HealthChanged
event, so they cannot interfere with one another.
→ there are no client scripts that fire to that server event
→ a module script, that was called by a server script, called by a local script runs the code where the player is shooting.
→ there are 250 bullets per reload, and each bullet shoots out per 0.03 seconds.
→ -> im sure that the amount of bullets being spawned simultaneously doesn’t affect the HealthChanged
, as it worked as I wanted it to work at least once.
code:
(i’ll move some parts of the code to another section, as i personally think they don’t have anything to do with the problem.)
local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local currentHealth = humanoid.Health
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.3, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut)
local start = false
local damageIndicatorGui
local damageIndicatorLabel
local numVal
local StrokeUI
local hasChanged
local ONLYONCE = false
local timer = 5;
local lastHp = humanoid.Health;
coroutine.wrap(function()
while humanoid.Health > 0 do
repeat
wait(1)
if start == true then
timer -= 1
end
until timer <= 0
warn("ENDED!!")
repeat wait(0.5) until damageIndicatorGui ~= nil
local tweenInfo2 = TweenInfo.new(
0.7, -- The time the tween takes to complete
Enum.EasingStyle.Circular, -- The tween style.
Enum.EasingDirection.In, -- EasingDirection
0, -- How many times you want the tween to repeat. If you make it less than 0 it will repeat forever.
false, -- Reverse?
0 -- Delay
)
local Tween = tweenService:Create(damageIndicatorLabel, tweenInfo2, {Rotation = 360}) -- Creates the tween with the TweenInfo and what properties you want to change
Tween:Play() -- Plays the tween
local guiDownTween = tweenService:Create(damageIndicatorGui, tweenInfo, {StudsOffset = damageIndicatorGui.StudsOffset + Vector3.new(0, -1, 0)})
guiDownTween:Play()
local textFadeTween = tweenService:Create(damageIndicatorLabel, tweenInfo, {TextTransparency = 1})
local sTween = tweenService:Create(StrokeUI, tweenInfo, {Transparency = 1})
textFadeTween:Play()
sTween:Play()
game:GetService("Debris"):AddItem(damageIndicatorGui, 0.3)
start = false
timer = 5
end
end)()
humanoid.HealthChanged:Connect(function(health)
if health < currentHealth and humanoid:GetState() ~= Enum.HumanoidStateType.Dead then
warn("TAKEN DAMAGE")
if(currentHealth < lastHp) then
timer = 5
else
lastHp = currentHealth
end
start = true
local healthLost = currentHealth - health
if char.HumanoidRootPart:FindFirstChild("DAMAGEINDICATEBILL") then
damageIndicatorLabel = char.HumanoidRootPart:FindFirstChild("DAMAGEINDICATEBILL").DAMAGEINDICATE
numVal = char.HumanoidRootPart:FindFirstChild("DAMAGEINDICATEBILL").DAMAGEINDICATE.DAMAGE
hasChanged = char.HumanoidRootPart:FindFirstChild("DAMAGEINDICATEBILL").DAMAGEINDICATE.hasChanged
damageIndicatorGui = char.HumanoidRootPart:FindFirstChild("DAMAGEINDICATEBILL")
StrokeUI = damageIndicatorLabel.UIStroke
ONLYONCE = true
else
warn("BE ONLY HERE ONCE")
numVal = Instance.new("NumberValue")
numVal.Name = "DAMAGE"
hasChanged = Instance.new("BoolValue")
hasChanged.Name = "hasChanged"
damageIndicatorGui = Instance.new("BillboardGui")
damageIndicatorGui.Name = "DAMAGEINDICATEBILL"
damageIndicatorGui.AlwaysOnTop = true
damageIndicatorGui.Size = UDim2.new(1.5, 0, 1.5, 0)
local offsetX = math.random(-10, 10)/10
local offsetY = math.random(-10, 10)/10
local offsetZ = math.random(-10, 10)/10
damageIndicatorGui.StudsOffset = Vector3.new(offsetX, offsetY, offsetZ)
StrokeUI = Instance.new("UIStroke")
StrokeUI.Thickness = 2.7
StrokeUI.Color = Color3.new(0, 0, 0)
damageIndicatorLabel = Instance.new("TextLabel")
damageIndicatorLabel.Name = "DAMAGEINDICATE"
StrokeUI.Parent = damageIndicatorLabel
damageIndicatorLabel.AnchorPoint = Vector2.new(0.5, 0.5)
damageIndicatorLabel.Position = UDim2.new(0.5, 0, 0.5, 0)
damageIndicatorLabel.Parent = damageIndicatorGui
damageIndicatorLabel.Size = UDim2.new(0, 0, 0, 0)
damageIndicatorGui.Parent = char.HumanoidRootPart
damageIndicatorLabel.TextScaled = true
damageIndicatorLabel.BackgroundTransparency = 1
damageIndicatorLabel.Font = Enum.Font.GothamBold
numVal.Parent = damageIndicatorLabel
numVal.Value = 0
hasChanged.Parent = damageIndicatorLabel
damageIndicatorLabel:TweenSize(UDim2.new(1, 0, 1, 0), "InOut", "Quint", 0.3)
end
coroutine.resume(coroutine.create(function()
numVal.Value = numVal.Value + healthLost
end))
local ValueOfHealthLost = numVal.Value
local ts = game:GetService("TweenService")
local ORIGINALnumVal = numVal.Value
damageIndicatorLabel.Text = string.format("%0.2f", ValueOfHealthLost)
hasChanged.Value = true
local guiUpTween = tweenService:Create(damageIndicatorGui, tweenInfo, {StudsOffset = damageIndicatorGui.StudsOffset + Vector3.new(0, 0.05, 0)})
guiUpTween:Play()
end
currentHealth = humanoid.Health
end)
TEXTCOLOR TWEENING:
coroutine.resume(coroutine.create(function()
if ValueOfHealthLost <= 22.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(255, 69, 69)}
):Play()
elseif ValueOfHealthLost <= 48.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(255, 150, 101)}
):Play()
elseif ValueOfHealthLost <= 73.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(188, 255, 65)}
):Play()
elseif ValueOfHealthLost <= 98.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(149, 200, 255)}
):Play()
elseif ValueOfHealthLost <= 123.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(146, 161, 255)}
):Play()
elseif ValueOfHealthLost <= 148.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(255, 197, 255)}
):Play()
elseif ValueOfHealthLost <= 173.5 then
ts:Create(
damageIndicatorLabel, -- instance
TweenInfo.new(
2, -- speed
Enum.EasingStyle.Circular, -- style
Enum.EasingDirection.Out, -- direction
0, -- repeat
false, -- boolean (reverse)
0 -- delay
),
{TextColor3 = Color3.fromRGB(255, 255, 255)}
):Play()
end
end))
Any help is appreciated. (sorry for the long code, i’m VERY unorganized.)