Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I make a check to make sure humanoids don't get hit in an amount of time?

Currently I am using a raycast module by TeamSwordphin, and I am trying to make this spin attack only damage a humanoid once it has been hit, then a little after, so the attack would not be in their hitbox anymore, until it comes back around.

newHitbox.OnHit:Connect(function(hit)
		print(hit)
		print(hit.Parent)
		local hum = hit.Parent:FindFirstChild("Humanoid")
		if hit.Parent.Name == game.Players:FindFirstChild(hit.Parent.Name) then

		elseif hum and hit.Parent.Name ~= game.Players:FindFirstChild(hit.Parent.Name)  then
			local tag = Instance.new("IntValue")
			tag.Name = "tag"
			
				tag.Parent = hum
		
			if tag.Value == 0 or tag.Value == nil then
				tag.Value = 1
				hum:TakeDamage(50)
				wait(.1)
				tag:Destroy()
			end

The problem I'm finding is that this seems unreliable. A problem that this may or may not cause is that not moving makes this not even fire, but also sometimes when I am moving, it just doesn't work. Any advice is helpful.

1 answer

✓ Accepted answer

There are comments in the code to help if needed, if you need more information on certain things used then:

GetPlayerFromCharacter

Coroutine

local Cooldown = 1 -- The seconds before it can be damaged again
local Debug = true

local AddCooldown = function(Humanoid)
	local Int = Instance.new("IntValue")
	Int.Name = "DamageCooldown"
	Int.Parent = Humanoid

	local Thread = coroutine.create(function()
		wait(Cooldown)
		
		if Debug then
			warn("Cooldown Taken Off")  -- some stuff to help
		end
		
		Int:Destroy()
	end)

	coroutine.resume(Thread) -- using coroutines means it ignores the wait inside of the function
end

newHitbox.Touched:Connect(function(Part)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(Part.Parent) -- more efficient method

	if Player then -- is it a player?
		if Player.Character then -- checking if player has a character
			if Player.Character:FindFirstChild("Humanoid") then -- does the character have a humanoid?

				local Humanoid = Player.Character:FindFirstChild("Humanoid")

				if not Humanoid:FindFirstChild("DamageCooldown") then
					AddCooldown(Humanoid) -- Adding DamageCooldown Value
					Humanoid:TakeDamage(50) 
					
					if Debug then
					   warn("Humanoid Took Damage") -- some stuff to help
					end
				end
			end
		end
	end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.