Hi!
Currently i’m working on fixing my hit detection for my weapons.
Right now, the guns don’t ignore the accessories of the player.
Here is the code i’m using to get the accessories.
local rayIgnoreList = {}
player.CharacterAppearanceLoaded:Connect(function(playerChar)
local humanoid = playerChar:WaitForChild("Humanoid")
if humanoid then
print("Humanoid Detected")
for i, v in pairs(humanoid:GetAccessories()) do
if v:IsA("Accessory")then
table.insert(rayIgnoreList, v)
print("Inserted: ", v)
end
end
end
end)
local function GetObject(Array) --- a quick function
for _, Val in pairs(Array) do -- for each index in the array given, do
return Val -- return the object/value
end
end
local object = GetObject(rayIgnoreList)
if object then
object.Handle.CanQuery = false
object.Handle.CanTouch = false
object.Handle.CanCollide = false
end
My issue is that it just plain doesnt work at all.
here’s my code for casting the bullets:
local bullet = game:GetService("ReplicatedStorage").Tracers.PistolTracer:Clone()
bullet.Name = "BulletTracer"
bullet.Size = Vector3.new(0.1, 0.1, 0.1)
bullet.Anchored = true
bullet.CanCollide = false
bullet.Parent = game.Workspace
bullet.CFrame = CFrame.new(origin, endposition) * CFrame.Angles(math.rad(math.random(-spread,spread)), math.rad(math.random(-spread,spread)), 0)
bullet.CollisionGroup = "Bullet"
local bulletHoleType = false
local Loop
--< HIT LOGIC INSIDE >--
Loop = game:GetService("RunService").Heartbeat:Connect(function(dt)
local function GetObject(Array) --- a quick function
for _, Val in pairs(Array) do -- for each index in the array given, do
return Val -- return the object/value
end
end
local object = GetObject(rayIgnoreList)
if object then
object.Handle.CanQuery = false
object.Handle.CanTouch = false
object.Handle.CanCollide = false
end
local TestRay = Ray.new(bullet.Position, bullet.CFrame.LookVector * velocity)
local PartHit, WorldPosition, WorldNormal = workspace:FindPartOnRayWithIgnoreList(TestRay, {game.Players.LocalPlayer.Character, script.Parent, object, object:FindFirstChild("Handle")})
bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
--< HIT LOGIC >--
if ( PartHit ) then
if ( PartHit ) then
createBulletHole(PartHit, WorldPosition)
end
if ( PartHit ).Parent:FindFirstChild("HumanoidRootPart") then
if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("HumanoidRootPart") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Torso") and damage ~= nil then
damage:FireServer(( PartHit ).Parent, (PartHit), 34)
local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
clone.Parent = game.Workspace
clone.Parent = nil
clone:Destroy()
bullet:Destroy()
Loop:Disconnect()
h.ImageTransparency = 0
tween:Create (h, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
else
bullet:Destroy()
Loop:Disconnect()
end
if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Head") and damage ~= nil or ((PartHit.Parent:IsA("Accessory")) or PartHit:IsA("Accessory") and damage ~= nil) or ((PartHit).Parent == object and damage ~= nil) then
if (PartHit).Parent == object and damage ~= nil then
damage:FireServer(( PartHit ).Parent.Parent, (PartHit), 42)
end
local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
clone.Parent = game.Workspace
clone.Parent = nil
clone:Destroy()
bullet:Destroy()
Loop:Disconnect()
hs.ImageTransparency = 0
tween:Create (hs, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
else
bullet:Destroy()
Loop:Disconnect()
end
if ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Left Arm") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Right Arm") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Right Leg") or ( PartHit ) == ( PartHit ).Parent:FindFirstChild("Left Leg") and damage ~= nil then
damage:FireServer(( PartHit ).Parent, (PartHit), 25)
local clone = player.PlayerGui.Template.Crosshair.hitmarker:Clone()
clone.Parent = game.Workspace
clone.Parent = nil
clone:Destroy()
bullet:Destroy()
Loop:Disconnect()
h.ImageTransparency = 0
tween:Create (h, TweenInfo.new(0.25), {ImageTransparency = 1}):Play()
else
bullet:Destroy()
Loop:Disconnect()
end
end
elseif bullet.Position.magnitude > 1000 and ( PartHit ) == nil then
bullet:Destroy()
Loop:Disconnect()
end
end)
Can anybody tell me what’s wrong with this? i’ve been struggling for weeks.
Thanks!