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

Why does the following code experience the listed issues?

Asked by
Romul 21
4 years ago

The following code is how I handle firing a projectile into a specified direction. The client determines the direction via the mouse location and fires the below event from the server side.

Under normal circumstances, this code works exactly as intended, however, I'm experiencing two notable issues that I need help with that I just can't seem to figure out myself..

1) The most notable issue is that the throwable object hangs up and stops due to interacting with itself. There are very random occurrences where if the projectile is sent at an empty space, there is a slight chance it will "hit" itself, causing the object to freeze in place and not proceed/interact with objects. I thought I handled avoiding this situation in the script but for some reason this still occurs. Could this possibly be due to the speed at which the code runs? I'm up for any advice to further troubleshoot and/or resolve.

2) When hitting another player from the front of their character, we experience the above hang up again. Hitting the player from the back of the character works fine. This is likely related to the above issue.

I appreciate any help you all may be able to provide. Let me know if I need to explain any portions of this code further or give feedback on troubleshooting.

Thanks! Romul

Bin.Throw.OnServerEvent:Connect(function(player,toss,pos)
    local Char = player.Character
    local WEP = game.ServerStorage.Weapons.Throwables:FindFirstChild(toss):Clone()
    WEP.Parent = game.Workspace
    WEP.Anchored = true
    WEP.CanCollide = false
    game.Debris:AddItem(WEP,15)
    WEP.CFrame = Char.HumanoidRootPart.CFrame*CFrame.new(2,0,-4)
    WEP.CFrame = CFrame.new(WEP.CFrame.p,pos)*CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
    local DB = false
    local STEP = false
    game:GetService("RunService").Heartbeat:Connect(function()
        if not DB and WEP then
            local BEG = WEP.Position
            local DIR = (pos-BEG).Unit*4
            local RES = workspace:Raycast(BEG,DIR)
            local INT = RES and RES.Position or BEG+DIR
            local DIS = (BEG-INT).Magnitude
            WEP.CFrame = CFrame.new(BEG,INT)*CFrame.new(0,0,-DIS/2)*CFrame.Angles(math.rad(90),math.rad(0),math.rad(0))
            if STEP == false and RES and RES.Instance ~= WEP and RES.Instance.Parent ~= player.Character and not RES.Instance.Parent:IsA("Accessory") and RES.Instance.Transparency < 1 then
                local part = RES.Instance
                STEP = true
                wait(.15)
                DB = true
                if WEP:FindFirstChild("Trail") then
                    WEP.Trail.Enabled = false
                end
                WEP.Anchored = false
                local WELD = Instance.new("Weld")
                WELD.Part0 = part
                WELD.Part1 = WEP
                WELD.C0 = part.CFrame:Inverse()
                WELD.C1 = WEP.CFrame:Inverse()
                WELD.Parent = WEP
                WEP.Massless = true
                local HUM = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid")
                if HUM then
                    HUM:TakeDamage(10)
                end
            end
        end
    end)
end)

2 answers

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

Sounds like your Raycast might be hitting your projectile causing it to hang

The raycast function can have a filter set so you can blacklist or whitelist

Blacklist is anything you dont want to check for so your ray goes through it

or

Whitelist is only check for things in the list

An example on how to hook up this ignore system (Taken from the wiki)

    -- Build a "RaycastParams" object
    local raycastParams = RaycastParams.new()
    raycastParams.FilterType = Enum.RaycastFilterType.Whitelist -- Change this to Blacklist
    raycastParams.FilterDescendantsInstances = {workspace.Model} -- enter in your newly cloned part
    raycastParams.IgnoreWater = true

    -- Cast the ray
    local part = workspace.Part
    local raycastResult = workspace:Raycast(part.Position, part.CFrame.LookVector*50, raycastParams) -- then add the raycastprams to your ray cast call

    -- Interpret the result
    if raycastResult then
        print("Object/terrain hit:", raycastResult.Instance:GetFullName())
        print("Hit position:", raycastResult.Position)
        print("Surface normal at the point of intersection:", raycastResult.Normal)
        print("Material hit:", raycastResult.Material.Name)
    else
        print("Nothing was hit!")
    end


hope this fixes your problem! :)

0
I completely forgot about blacklisting!! You are a genius my friend, this indeed corrected the initial issue. I will have to test the player interaction later tonight but I'm sure this will resolve both issues! Thanks!!! Romul 21 — 4y
Ad
Log in to vote
0
Answered by
Romul 21
4 years ago
Edited 4 years ago

I inserted the following code before the service is ran and it is no longer hitting itself. Thanks TGazza!

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {WEP}

Line 16 in the OG code will also need to state:

local RES = workspace:Raycast(BEG,DIR,raycastParams)
0
lol your welcome! :D TGazza 1336 — 4y

Answer this question