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

Use Raycast or Collisions?

In a game with many zombies, should I find and damage the player by using raycast(from zombie torso to lookVector) or by Touched() event?

2 answers

For my zombies i check to see if the player is within a certain distance then a run an attack function

local dist = zombie.HumanoidRootPart.Position - closestPlayer.Character.HumanoidRootPart.Position).magnitude -- gets the distance between the zombie and the player
if dist <= 15 then
	if canAttack then -- if the zombie can attack
		canAttack = false
		Attack() -- your attack function		
		wait(attackCooldown)
		canAttack = true
	end
end

You just need to update this every second or when an event fires for it to work.

5 comments

0

I think this will put too much load on the game. I want to know the best and most efficient way of making a zombie attack a player.

0

nice place this under Humanoid.Running event

0

@Francis I think his method is better than using touched event because touched interacts with all parts. So if the zombie is walking the legs will touch the ground firing the event constantly.

0

^You don't necessarily need to run the Touched event on all parts? Touched is very efficient but not always the most reliable...if your zombie is good at tracking then it should work fine

0

You need to run is on all the parts because if only the HRP or Torso is using Touched, the player can just jump on top of the zombie's head and not take any damage.

If you want is to count if the zombie touches the player, use :Touched().

If this helped you, make sure to accept the answer, it will help us both out.

2 comments

0

This did not help, sorry. You misunderstood my question. I was asking what is better to use - Touched() or Raycast to check if player is right next to the zombie, so that the zombie could do damage.

0

Player touching Zombie or Zombie touching Player, same thing... makes no difference.

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