I’m working on a 3rd person gun that shoots at players when clicked by the mouse. (No camera lock and 3rd person.) Players can currently click on other players and shoot them even if they are behind a wall. I’ve tried to work with Raycasting, but I can’t seem to work it out. Are there alternative methods I can use for this? If not, how could I go about implementing Raycasting into my gun?
Revolver > LocalScript (Fire)
--variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false
--
script.Parent.Activated:Connect(function()
local target = mouse.Target
if target.Parent:FindFirstChild("Humanoid") and debounce == false then
script.Parent.RemoteEvent:FireServer(target.Parent)
debounce = true
wait (1.5)
debounce = false
end
end)
First of all, you should use Raycasting on the server (to prevent exploiting and fix being able to kill things through walls) and secondly, To do bullet penetration, just check if the surface the ray hit was small enough, then fire a second ray on the other side of the wall.
I’ve rewrote the script, and it now successfully works with ray casting! Players cannot shoot through walls, but if their gun tool is clipping through the wall, it will let them do so. Any ideas on how to fix this?
Gun Main Script:
--variables
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local debounce = false
local Tool = script.Parent
local User
--
Tool.Equipped:connect(function(mouse)
User = Tool.Parent
mouse.Button1Down:connect(function()
local Ray = Ray.new(Tool.Handle.CFrame.p,(mouse.Hit.p-Tool.Handle.CFrame.p).unit*300)
local Hit,Position = game.Workspace:FindPartOnRay(Ray,User)
if Hit and debounce == false then
if Hit.Parent:FindFirstChild("Humanoid") then
Hit.Parent.Humanoid:TakeDamage(30)
debounce = true
wait(1.5)
debounce = false
end
end
end)
end)
So basically, you can mark certain walls as penetrable, which in that case you can just ignore all of them using RayCastParams, also, use the newer workspace:Raycast() system as Workspace:FindPartOnRay() is deprecated. You could also loop through all of the thin parts in workspace and then filter those out at the start of the game.
You could also measure the thickness of the part you just hit, and if it is thin enough you can refire the ray but with that specific part filtered out.
This is a bad scripting practice, consider moving it to the server to prevent a major exploit vulnerability in your game. Since the player can just give whatever player they want to kill and the server will kill them without question.
So basically, if you do not understand FilteringEnabled and how multiplayer games work, what you are actually seeing when you play a game is called a “client” and the server is the middleman. Everything that happens on the server will also happen on a client, but nothing that happens on a client is replicated to the server. So, since this is a local script, only the person who fired the gun is actually able to see the damage, and the person will not die when their health reaches zero because they didn’t actually die on their client. A good article on this is from the unofficial roblox wiki.
This advice is the wrong approach. Raycasting on the server will result in latency. You need to raycast on the client to minimize latency and then validate that on the server. The most practical way to do a gun system is with raycast.