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..
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.
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)

1 comment
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!!!