Raycasting, getting a player when a Ray hits them

now it prints out my username lol

Then you can just find the humanoid of that player’s character and damage it.

Like

	local character = RayInstance.Parent.Parent
	if character then
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			humanoid:TakeDamage(10)
		end
	else
		character = RayInstance.Parent
		if character then
			local humanoid = character:FindFirstChild("Humanoid")
			if humanoid then
				humanoid:TakeDamage(10)
			end
		end

if this doesn’t work i have another method but omg bruva why isnt it workingahhhhhh

2 Likes

it works now but it sometimes doesn’t damage the player for some reason it only damages me when i gets too close to it

Like how close?

uh yeah I like cars they’re cool (char lim it)

1 Like

like if i’m like 10 studs close to it

It’s only detecting for the direction you put it as? Try increasing the direction variable

	local direction = part.CFrame.LookVector * 250
1 Like

Wait I just realized something…

1 Like

yeah it sometimes damages the player

this will do nothing if the humanoid isnt nill

1 Like

weird it doesn’t damages the player when i face sideways towards it

What’s your script look like atm?

1 Like

update

local part = script.Parent
local partmodel = workspace.Workerthrow



while wait(1) do
	for i, v in pairs(game.Workspace:GetChildren()) do
		local human = v:FindFirstChild("Humanoid")
		local torso = v:FindFirstChild("Head")
		if human and torso and human.Health > 0 then
			if (torso.Position - part.Position).magnitude < 100 then
				part.CFrame = CFrame.new(part.Position, torso.Position)
			end	
		end
	end
	local origin = part.Position
	local direction = part.CFrame.LookVector * 250

	local raycastParams = RaycastParams.new()

	raycastParams.FilterDescendantsInstances = {part, partmodel}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local raycastResult = workspace:Raycast(origin, direction, raycastParams)
				
	if raycastResult then
		local RayInstance = raycastResult.Instance
		print(RayInstance)
		if RayInstance then
			local character = RayInstance.Parent.Parent
			if character then
				local humanoid = character:FindFirstChild("Humanoid")
				if humanoid then
					humanoid:TakeDamage(10)
				end
			else
				character = RayInstance.Parent
				if character then
					local humanoid = character:FindFirstChild("Humanoid")
					if Humanoid == nil then
						Humanoid = RayInstance.Parent.Parent
						if Humanoid then
							Humanoid:TakeDamage(10)
						end
					end
				end
			end
		end
	end
end

Hmm. What is it printing when you are sideways?

1 Like

it’s either Head or Handle lol

That’s weird. Possible the ray? I’m not sure.

1 Like

Also, what are you making?

i use youtube videos to help me out sometimes lol

1 Like

i’m making an “turret” pretty much a npc throwing boxes at you i got everything else down it’s just this

whoa idk how that happened tbh

Hm. I know thedevking made a video on one of these.

I sadly can’t help you anymore (because I have to go lol), but maybe watching the video will help? I’m sorry I don’t know what to tell you .

1 Like

ah it’s fine thanks for everything though! :smiley:

1 Like