Issues with FindPartOnRay

Currently, the gun i’m making uses workspace:FindPartOnRay() to see if it hits any part that would be in its path, but it doesn’t always work as intended (especially far away from the origin 0,0,0). It will simply ignore parts, but sometimes getting very close will actually make it work.

Intended- https://gyazo.com/4bdb62495d49b10206a4a5f21bade85e
Unintended- https://gyazo.com/03e6910b4bfe5a12d38dcec31569c02d

Can you please post your code, so we can give you more solutions on how to fix it?

1 Like

script.Parent.FireGun.OnServerEvent:Connect(function(player,Hit)
local pos = Hit.p
local attach1 = Instance.new(“Attachment”)
local attach0 = Instance.new(“Attachment”)
attach0.Position = (script.Parent.Handle.CFrame*CFrame.new(0,0.2,-1.25)).p
attach1.Position = (CFrame.new(attach0.Position,pos)*CFrame.new(0,0,-script.Parent.Range.Value)).p
end)

Hit will always be a CFrame (i check for it in the actual script)

As for getting what its hitting,

local r = Ray.new(attach0.Position,attach1.Position)
local part,p = workspace:FindPartOnRay(r,player.Character)

Okay first of all, you need to get the position on where the mouse points.

First thing is to insert a RemoteEvent inside the ReplicatedStorage, then write the below code inside your gun localScript:

script.Parent.Equipped:Connect(function(MouseDi)
 MouseDi.Button1Down:Connect(function()
  game.ReplicatedStorage.EventName:FireServer(script.Parent.bullet.Position,MouseDi.Hit.Position)
 end)
end)

I would recommend you to work with a GlobalScript(a normal script) to create the ray, deal damage to the player etc.

So insert a script inside the ServerScriptService and just write the below code:

game.ReplicatedStorage.EventName.OnServerEvent:Connect(function(char, FromPos, ToPos)
local RayC = Ray.new(FromPos,(ToPos-FromPos).unit*100)
local hitPart, Position = game.Workspace:FindPartOnRay(RayC, char.Character, false, true)
if hitPart then 
	local human = hitPart.Parent:FindFirstChild("Humanoid")
	if human then
		human:TakeDamage(50)
	end
end
local LazerVisual = Instance.new("Part")
LazerVisual.Parent = game.Workspace
LazerVisual.CanCollide = false
LazerVisual.Anchored = true
local Distance = (ToPos-FromPos).magnitude
LazerVisual.CFrame = CFrame.new(FromPos,Position)*CFrame.new(0,0,-Distance/2)
LazerVisual.Size = Vector3.new(0.1,0.1,Distance)
game.Debris:AddItem(LazerVisual,0.1)
end)

How would I verify if they’re holding the tool or not?

Do you want to verify if the tool is Equipped for a reason or because you are worried that the player will be able to shoot without equipping the tool?
Because if that’s the case, player won’t be able to shoot without having the tool equipped since it won’t trigger the RemoteEvent if the tool isn’t equipped.

Script.Parent.Equipped:Connect(function(MouseDi)

Everything that goes below there, works only if the player has the tool equipped.

I’m more worried about exploiters being able to shoot when they shouldn’t be able to, also it doesn’t seem like a good idea that the client can choose where the the ray should start from (would be able to shoot where they shouldn’t be able to normally).

Shooting direction is client input, therefore it should be handled from the client. The server can’t and shouldn’t be trying to determine that; it should instead be validating the ray. The same goes for determining when to shoot; keep effects on the client and important functionality on the server. If you have an effects replication model, don’t replicate anything to other clients when a shot is fired when it shouldn’t be.

Ninja’s solution checks out, minus the lack of security in the remote and the fact that the ray tracer is being created on the server (it should be created on the client).

1 Like

That’s not the issue, the client being able to determine where the ray should start is the issue.
This means an exploiter could shoot outside the gun’s range, behind walls, or generally where they normally couldn’t because they moved the start of the ray so it looks legitimate.

Don’t let the client determine where the ray starts then? I’m not sure why your raycasts are being handled like that. You should only be using the player’s mouse position to be determining where the ray should point from a given origin (i.e. a barrel).

My response doesn’t change at all.

1 Like