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.
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
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!
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.