Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Raycast gun not working with FilteringEnabled?

Asked by 6 years ago
Edited 6 years ago

I scripted my raycast gun to work with filteringenabled, and it partially works. When testing, the user sees the parts but does not take damage. The damage happens only on the client, not the server. Here are my scripts.

In ReplicatedFirst, I have a folder with a module script with this code:

local module = {}
function module.GunShooter(scarpt)
    local tool = scarpt.Parent
local player = game.Players.LocalPlayer
local modulescript = require(game.ReplicatedStorage.WepHandler.UziHandler)
local mouse = player:GetMouse()
local user
local down = false

tool.Equipped:connect(function(mouse)
mouse.Icon = "rbxasset://textures\\GunCursor.png" 
    user = tool.Parent
    mouse.Button1Down:connect(function()
down = true
        --make and do a hit test along the ray
while down == true do
wait(0.0714285714)
game.Workspace.Shot:Play()

        local ray = Ray.new(tool.Handle.CFrame.p, (mouse.Hit.p - tool.Barrel.CFrame.p).unit*300)
        local hit, position = game.Workspace:FindPartOnRay(ray, user)

        --do damage to any humanoids hit
        local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
        if humanoid then
            humanoid:TakeDamage(10)
        end

local distance = (position - tool.Barrel.CFrame.p).magnitude
        local rayPart = Instance.new("Part", user)
        rayPart.Name          = "RayPart"
        rayPart.BrickColor    = BrickColor.new("Bright red")
        rayPart.Transparency  = 0.5
        rayPart.Anchored      = true
        rayPart.CanCollide    = false
        rayPart.TopSurface    = Enum.SurfaceType.Smooth
        rayPart.BottomSurface = Enum.SurfaceType.Smooth
        rayPart.formFactor    = Enum.FormFactor.Custom
        rayPart.Size          = Vector3.new(0.01, 0.01, distance)
        rayPart.CFrame        = CFrame.new(position, tool.Barrel.CFrame.p) * CFrame.new(0, 0, -distance/2)

        --add it to debris so it disappears after 0.1 seconds
        game.Debris:AddItem(rayPart, 0.1)


mouse.Button1Up:connect(function()
down = false
end)
end
end)
end)

end
return module

To call it, I have a local script in the tool that says this:

local modulescript = require(game.ReplicatedStorage.WepHandler.UziHandler)
modulescript.GunShooter(script)
0
requie() in a localscript is the same as typing the code there. Why would it replicate... cabbler 1942 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago

"the user sees the parts but does not take damage. The damage happens only on the client, not the server."

This is because the ModuleScript is being run entirely on the client. If you want anything to occur on the server (ex the part appearing, the damage for the humanoid), you'll need to send information to the server via a RemoteEvent. If you want your place to be exploit-proof, make sure to validate the information received on the server through this event -- ex, an exploiter might figure out how to send tons of information through this event so as to kill everyone in a fraction of a second, unless the server makes sure to keep track of cool-down times (for example).

Ad

Answer this question