"In Combat" Tag

So, im working on this hardcore game, where if you were to hit somebody you’d be put “in combat” but I dont understand how i’d make an in combat tag. All I want it to do is show a GUI once you’ve been put in combat, and add a value inside the character called “InCombat”.
InCombat
I already got it so where if i were to hit somebody, that tag would show on our screens, but i wanted to figure out how to make it go away after a certain amount of time without being hit. So around 30 seconds without being hit, your in combat tag will go away, but if you were to get hit, or hit someone again, the tags timer would reset.


This is my script, where the GUI turns visible,

And this is part of my combat script that fires the remote event on the client.
I assume i’d have to use tick, but i tried and it didnt work.

Should be the same concept as a combo timer.

Just use a timer module or method doesn’t really matter to keep track of the time and execute code when it runs out.

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)

2 Likes

Thanks, i’ll try, and then ill post the results once im finished

Uh, it sort of works?? But when I attack the other person, i believe the while wait loop interupts my other scripts. Since while wait doesnt run passively, it stops everything past that point and just keeps running the loop. Know how i’d go about fixing this??
EDIT: Actually, im going to try to tweak something

Thanks, got it working! Do you mind explaining task.wait() because I dont like using code I can’t understand

Yeah sure! task.wait() is basically just the optimized version of wait(). task.wait() is faster and has better performance when compared to wait(). There is mainly no reason to use wait() because task.wait() supersedes it.

1 Like