How would i go about making my monster only able to hit the player once? [FIXED]

I would like to make my monster only be able to hit the player once, however with the method ive found it just makes the monster not able to hit them multiple times every raycast. It would be helpful if any of you could just tell me what i should try, but if you could fix the script that would be wonderful.

function rusher.FindPlayers(model, damage, raycast)

	if not model then return end
	local players = playerService:GetPlayers()
	local charactersTable = {}

	for i, player in ipairs(players) do

		if player.Character then
			table.insert(charactersTable, player.Character)
		end

		local overlapParams = OverlapParams.new()
		overlapParams.FilterType = Enum.RaycastFilterType.Include
		overlapParams.FilterDescendantsInstances = charactersTable

		local collisions = workspace:GetPartsInPart(model.Hitbox, overlapParams)

		for index, obj in ipairs(collisions) do
			if obj.Name == "HumanoidRootPart" then
				print("InHitbox")
				
				if not raycast then
					print("BypassedRaycast")
					obj.Parent.Humanoid.Health -= damage
				end 
				
				local rayDirection = obj.Position - model.PrimaryPart.Position
				local result = workspace:Raycast(model.PrimaryPart.Position, rayDirection)

				if result and result.Instance then

					local hit = result.Instance
					
					if hit == obj or hit.Parent == obj.Parent then
						if table.find(charactersTable, obj.Parent) ~= nil then
							obj.Parent.Humanoid.Health -= damage
							table.remove(charactersTable, table.find (charactersTable, obj.Parent) )
						end
					end			
				end
			end
		end

	end
end

Also this may be the wrong category? Im not sure… Also the monster is like rush from doors, if that helps.

Is it like a group of players or just one player? Does it go away after hitting one player or does it have to hit all players?

1 Like

Add a debounce.

local hasHitDeb = false --The debounce variable

if not hasHitDeb then --put this inside the event that detects when the monster hits the player
hasHitDeb = true

--Add whatever happens when the monsters hits
end

check the players character is humanoidstateType is dead or use an attribute to check their alive e.g.
local isAlive=player:GetAttribute(“Alive”)
if isAlive then
–do something
end

It can hit multiple players, yes.

The player’s will be able to increase their health to over 150.

Will not work since it can hit multiple players.

Oh yeah sorry i didnt realise your using the function GetPartsInParts()

I think the problem may be due to the raycast as you both have to deal damage.Say you set the variable raycast to true it would deal double damage as your also creating a ray again within that function.

You could just make a table empty table, then once a player is hit, then add them to that table, but when hit then check if player is in table, if the player is in a table, then don’t run it!

the problem is he is using both GetPartsInPart() and raycasting both.

Instead of using a boolean debounce, you can use a list: store a list of the players that were already hit and use that list as a debounce before hitting characters.

You can get a player from a character using Players:GetPlayerFromCharacter.

True, however none of my entities yet have the raycast set to false

Thank you, i added a ignore list (which checks for the character but works the same), and it worked!

1 Like