I don’t have much experience in this, and I’m not 100% sure that this will work, but I think you can do something like this:
First, I’d recommend adding a Bool value
to every player. This can be done using a .PlayerAdded event. We are going to add 2.
local Players= game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local CombatTag = Instance.new("BoolValue")
CombatTag.Name = "InCombat"
CombatTag.Parent = plr
local ResetTimer = Instance.new("BoolValue")
ResetTimer.Name = "ResetTimer"
ResetTimer.Parent = plr
--also anything else you would want to do.
end)
end)
The combat tag bool value will be enabled when the player gets in combat. The reset timer Boolean will be activated when the player is already in combat, and needs to reset the timer.
Now that we have a combat tag, we’ll need to enable it whenever the player gets “in combat” so, whenever you detect that a player is in combat, you will turn this to true.
Here is an example of this (I’d recommend inserting this somewhere where it can go along with your code in the server script you provided)
local CombatTag = plr:FindFirstChild ("InCombat")
local ResetTimer = plr:FindFirstChild("ResetTimer")
if CombatTag and ResetTimer then
if CombatTag ~= true then —Combat tag should always exist, but better safe than sorry. This will also make sure that the remote event will not fire more than needed
CombatTag.Value = true
—fire your remote event here
else -- if the combat tag is true, so instead of turning it on, we reset timer
ResetTimer.Value = true
end
end
now, this value will be turned to true whenever the player gets hit, but we need to make the timer actually work. But before that, I recommend you change your for loop into tweens since they are more optimal and simple to use
local event = game:GetService("ReplicatedStorage").Remotes.Client.InCombat
local TweenService = game:GetService("TweenService")
event.OnClientEvent:Connect(function()
TweenService:Create(script.Parent.InCombat,TweenInfo.new(--change to your liking),{TextTransparency = -- Change to what the goal transparency should be})Play()
end)
so now, finally for the combat tag part, you would do something like this:
local CombatTag = plr:FindFirstChild ("InCombat")
local ResetTimer = plr:FindFirstChild("ResetTimer")
if CombatTag and ResetTimer then
if CombatTag ~= true then —Combat tag should always exist, but better safe than sorry. This will also make sure that the remote event will not fire more than needed
CombatTag.Value = true
—fire your remote event here
--when you fire the client:
local time = 30
local connection
connection = ResetTimer:GetPropertyChangedSignal("Value"):Connect(function()
if ResetTimer.Value == true then
ResetTimer.Value = false
time = 30
print('timer changed ')
end
end)
while time > 0 do
time -= 1
task.wait(1)
print(time)
if time == 0 then
connection:Disconnect() -- they are no longer in combat so the function shall be disconnected to avoid any unnecessary functions in your game
-- now do whatever you want here! (turn the combat tag off, etc)
end
end
else -- if the combat tag is true, so instead of turning it on, we reset timer
ResetTimer.Value = true
end
end
And after that, you should have a proper working in combat effect! I hope this can help you, and if there are any typos or code that isn’t working, let me know! (Also, since this was typed on mobile, some things are incorrectly formatted such as comments and quotation marks. If this happens, just edit them yourself)