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

Casted ray isn't always picking up that it has connected with a player?

Asked by 3 years ago

Currently I am scripting a weapons handler script for a game I am working on. The guns shoot out a beam, which travels a set distance and lingers for a very short period of time. When this beam is fired, a ray is also fired too, that follows the beam, to detect if a character is hit to then deal damage. However, it doesn't always deal damage. It used to work every time when the ray was drawn between the targetlocation and the handle, however, the script was modified have spray, and since then the rays have been inconsistent, even with the weapon that has no spray. This is the script:

local GunData = require(game.ReplicatedStorage.GunData)
local rep = 1
game.ReplicatedStorage.ShootGun.OnServerEvent:Connect(function(Player, TargetLocation, Handle)
    if Player.Character == nil then
        return
    end

    if Handle then 
        local Tool = Handle.Parent
        if Tool:IsA("Tool")then
            if Tool.Parent == Player.Character then
                local relevantData = GunData[Tool.Name]
                if relevantData ~= nil then
                    for count = 1,relevantData.REP do
                        Handle.Sound:Play()
                        local Beam = Instance.new("Part", workspace)
                        Beam.Color = relevantData.COLOR
                        Beam.FormFactor = "Custom"
                        Beam.Transparency = 0.25
                        Beam.Material = "Neon"
                        Beam.Anchored = true
                        Beam.CanCollide = false
                        local Distance = (TargetLocation - Handle.Position).magnitude
                        if Distance >= relevantData.RANGE then
                            Distance = relevantData.RANGE
                        end
                        Distance = Distance + 5
                        local NewAngle = CFrame.Angles(math.rad(math.random(-relevantData.ANGLE,relevantData.ANGLE)),math.rad(math.random(-relevantData.ANGLE,relevantData.ANGLE)),math.rad(math.random(-relevantData.ANGLE,relevantData.ANGLE)))
                        Beam.Size = Vector3.new(relevantData.WIDTH, relevantData.WIDTH, Distance)
                        Beam.CFrame = CFrame.new(Handle.Position, TargetLocation) * CFrame.new(0, 0, -Distance/2)
                        local offset = Handle.CFrame:Inverse()*Beam.CFrame
                        local handleStartCFrame = Handle.CFrame
                        local newCFrame = handleStartCFrame*NewAngle
                        Beam.CFrame = newCFrame*offset
                        game.Debris:AddItem(Beam, 0.1)

                        local NewRay = RaycastParams.new()
                        local RayDirection = Beam.CFrame.LookVector * Distance

                        NewRay.FilterDescendantsInstances = {Player.Character}

                        local Result = workspace:Raycast(Handle.Position, RayDirection, NewRay)

                        if Result then
                            if Result.Instance then

                                local humanoid = Result.Instance.Parent:FindFirstChild("Humanoid") or Result.Instance.Parent.Parent:FindFirstChild("Humanoid")
                                if humanoid then
                                    humanoid.Health -= relevantData.DMG
                                end
                            end
                        end
                        if relevantData.DELAY ~= 0 then
                            wait(relevantData.DELAY)
                        end
                    end
                end


            end
        end
    end

end)

If anyone knows why it isn't always damaging the player, then any pointers in the right direction would be appreciated

0
to help make it clear, the ray cast starts at line 37 DBlockSouthAmerica 0 — 3y

Answer this question