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

Why doesn't my ray casting script work in game but in studio?

I'm not really good at raycasting so I don't know how to do stuff. Really I wanted to use Filtering Enabled but I can't access my weapon with a server script due to it being in the camera unless I learn. Any help would be appreciated

function createRay()
	
			local muzzle = gun[gun.Name].Flash
	local ray = Ray.new(mouse.Hit.p,muzzle.CFrame.lookVector*999)
	local hit,pos = workspace:FindPartOnRayWithIgnoreList(ray,{char})
	
	if hit then
			local human = hit.Parent:FindFirstChild("Humanoid")
			if human then
			print(hit.Name)
			if human.Health >0 then
			player.PlayerGui.HUD.Crosshair.Hit:Play()
			player.PlayerGui.HUD.Crosshair.Frame.Hitmarker.Visible = true

			if hit.Parent == "Head" then
			human:TakeDamage(s.dmg * s.multi)
			
			elseif hit.Parent:IsA("Part") then
			human:TakeDamage(s.dmg * s.damage)
			
			elseif hit.Parent:FindFirstChild("Humanoid") then
			human:TakeDamage(s.dmg)

			
			end
			end
			end
			end
end

Sometimes the ray wouldn't detect the head even though I'm aiming straight at it. Not sure why? Even the Upper Torso. Maybe I should try R6 instead

1 answer

✓ Accepted answer

The Direction of your ray constructor is wrong, using lookvector will only make the ray cast straight in front of your head, you need to get the direction of where the ray is supposed to cast

Im assuming your char is referencing the player character

local ray = Ray.new(mouse.Hit.p,(mouse.hit.p-char.Head.Position).unit*999)

Here i made a gun to show what will happen if you use lookvector: https://gyazo.com/cd5015c1afdf5437918c00101ce94eee

And if you constructed the ray correctly: https://gyazo.com/2f957a27cd63375072a9fb1aea9a7a45

Note that the beams are being blocked by other beams because i didnt add beams to the ignorelist

7 comments

0

I can't test it due to roblox being down but thanks for the help!

0

No problem, any errors with your script feel free to come back and ask, so long as you attempted it

0

of course! I always try things first

0

oh I almost forgot the raycast won't do damage in game though what do I have to do here?

0

change the health property of the humanoid, you already got the humanoid in the variable human

0

lol, at your video you forgot to use the ":destroy" thing, so the bullet will just float arround for ever

0

yes i did that intentionally for the sake of this demonstration

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