Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Raycasting gun shooting through objects and not damaging humanoids? Im stuck.

Asked by 13 days ago

So I used Roblox's wiki raycasting gun, as I wanted to use something that for sure worked with no problems. I have tried this about a year ago, but never did it and gave up Roblox Studio. Anyways, I finally did it, I made the gun shoot through Filtering Enabled. However, the Raycast or the visible part "bullet" is going THROUGH objects and is not damaging any other players. I know it has something to do with my Remote Event as the default wiki code works just fine. Any help please? Thank you very much.

--Local Script inside tool

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer


local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gunEvent = ReplicatedStorage:WaitForChild("GunEvent")




tool.Equipped:connect(function(mouse)
    print("Tool equipped!")

    mouse.Button1Down:connect(function()
    gunEvent:FireServer(tool.Handle.CFrame.p,mouse.Hit.p, player)
    end)
end)

--script inside server script service

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local gunEvent = Instance.new("RemoteEvent", ReplicatedStorage)
gunEvent.Name = "GunEvent"

local function bulletShot(player, position_1, position_2, thePlayer)
    print("function completed")
    local ray = Ray.new(position_1, (position_1 - position_2).unit * 300)

    local part, position = workspace:FindPartOnRay(ray, player.Character, false, true)

        local beam = Instance.new("Part", workspace)
        beam.BrickColor = BrickColor.new("Bright red")
        beam.FormFactor = "Custom"
        beam.Material = "Neon"
        beam.Transparency = 0.25
        beam.Anchored = true
        beam.Locked = true
        beam.CanCollide = false

        local distance = (position_1 - position).magnitude
        beam.Size = Vector3.new(0.3, 0.3, distance)
        beam.CFrame = CFrame.new(position_1, position) * CFrame.new(0, 0, distance / 2)

        game:GetService("Debris"):AddItem(beam, 0.1)


    if part then
        local humanoid = part.Parent:FindFirstChild("Humanoid")

        if not humanoid then
            humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
        end

        if humanoid then
            humanoid:TakeDamage(30)
        end
    end

end

gunEvent.OnServerEvent:Connect(bulletShot)

thank you very much

Answer this question