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