Enemy damage indicator causing lag in-game

Right now, I am trying to add a damage indicator to make it more satisfying to hit enemies in-game by using a damage indicator. The problem is that when the tween to change enemy color plays, it lags the game a lot.
I’ve tried to use different methods, like a for loop, but it had the same issue (though I much prefer how it looks, and it’s a bit less laggy)
Here is what I had.

-- Tween (more laggy, not the whole script, just the important part)
script.Parent.Parent.Humanoid.HealthChanged:Connect(function()
	if db then
		db = false
		if script.Parent.Parent.Humanoid.Health < oldHp then
			local tweeninfo = TweenInfo.new(0.1,Enum.EasingStyle.Quad,Enum.EasingDirection.In,0,true)
			local property = {
				HeadColor3 = Color3.fromRGB(200,0,0),
				LeftArmColor3 = Color3.fromRGB(200,0,0),
				RightArmColor3 = Color3.fromRGB(200,0,0),
				LeftLegColor3 = Color3.fromRGB(200,0,0),
				RightLegColor3 = Color3.fromRGB(200,0,0),
				TorsoColor3 = Color3.fromRGB(200,0,0),
			}
			
			local tween = tws:Create(script.Parent,tweeninfo,property)
			tween:Play()
			wait(0.3)
		end
		
		oldHp = script.Parent.Parent.Humanoid.Health
		db = true
	end
end)


--for loop (less laggy, looks better, still too laggy to play though)
script.Parent.Parent.Humanoid.HealthChanged:Connect(function()
	if db then
		db = false
		if script.Parent.Parent.Humanoid.Health < oldHp then
			local r = 20 / 255
			
			for i=1,5 do
				for _, enumItem in pairs(Enum.BodyPart:GetEnumItems()) do -- I took this from a staff post on the devforum
					script.Parent[enumItem.Name .. "Color3"] = Color3.new(script.Parent[enumItem.Name .. "Color3"].R + r, script.Parent[enumItem.Name .. "Color3"].G, script.Parent[enumItem.Name .. "Color3"].B)
				end

				wait(0.025)
			end
			for i=1,5 do
				for _, enumItem in pairs(Enum.BodyPart:GetEnumItems()) do
					script.Parent[enumItem.Name .. "Color3"] = Color3.new(script.Parent[enumItem.Name .. "Color3"].R - r, script.Parent[enumItem.Name .. "Color3"].G, script.Parent[enumItem.Name .. "Color3"].B)
				end

				wait(0.025)
			end
			wait(0.3)
		end
		
		oldHp = script.Parent.Parent.Humanoid.Health
		db = true
	end
end)

If you can spot an issue, please tell me! Remember, the problem is just lag, not any errors.

Anyways, thanks.

Is this a local script? Ideally because it is just a visual thing, you would want the server to say “Hey client-erino, the person or players who you just damaged is Billy, make them flash for a second!”

If you notice that all clients are lagging when this is happening it might be cause the server is struggling, and this would be the case if there were multiple enemies/players being shot.

1 Like

This is a server script, although I wouldn’t want to fire a remoteEvent every time something gets hit. Mostly just because I generally avoid them, because I see them as inefficient, although I don’t actually know whether or not they are.

Basically, will the remote events themselves cause lag?
Also, (I promise it’s the last thing) would there be a way to make sure if a client’s memory usage is too high, to turn off damage indicators, say, 50% of the time?

  1. You don’t have to use Remote Events for Server → Client communication. You can create your own implementation without throttling the network by e.g. creating a “FakeRemotes” Folder, adding objects to it and having the Client listen to FakeRemotes.ChildAdded events, then replicating them accordingly.

  2. Remote Events are not inefficient. They are optimized to handle huge amounts of traffic & data and - don’t take this as a personal attack - any Remote Event inefficiency boils down to ROBLOX’s Servers malfunctioning or simply a misuse or abuse of the feature.

  3. You can turn off 50% of your damage indicators by checking for the client’s FPS:
    https://developer.roblox.com/en-us/api-reference/function/Workspace/GetRealPhysicsFPS

-- Some event function
if workspace:GetRealPhysicsFPS() < 30 then -- Sub 30 FPS is worse than older generation consoles
	if math.random(1,2) == 2 then -- 50/50 chance,
		return -- Do not render the effect:
	end
end
-- Otherwise run the code.
-- end event function
2 Likes

Alright, thanks!

I never knew about the :GetRealPhysicsFPS() function, that should help a lot.
Also, I think I’ll use normal remoteEvents for now and see how it goes.

Edit: I might end up doing this later, but I just used a highlight. This answer could help someone else, though, so that’s why it’s accepted.