My game has a gun tool used by player which has FindPartOnRayWithIgnoreList() in it’s scripts and that creates a problem with shooting npc. Sometimes when I equip the tool npc stops shooting at me because raycasts are getting ignored, I have no idea what to do since rewriting a script with 3000 lines is not an option for me.
Is there a way to make this npc work in a different way so FindPartOnRayWithIgnoreList() function inside of player’s gun won’t cause any trouble? Please give me any suggestions
shoot.rbxm (17.7 KB)
LoadLibrary.rbxmx (249.0 KB)
Please describe your issue in the title of your post. It doesn’t tell readers what your issue is so people might not click on it.
Why is the script 3000 lines long? That seems a bit excessive for a gun shooting system.
You need to find out why a player equipping the tool stops the NPC from shooting. Look at your code to see what happens when it’s equipped.
Does the code take the gun you are equipping away from the NPC?
Are there multiple sections of code that run when the player equips the gun? It should only be one section of code in the script.
It is way easier to rescript the npc so this is why I am asking if there is a way to make the npc shooting script work without usage of raycasts since player’s tool causes the raycasts from npc’s script to be ignored
local RbxUtility = require(game:GetService("ReplicatedStorage"):
WaitForChild("LoadLibrary"):WaitForChild("RbxUtility"))
wait(.1)
local ammo=29
local w=.14
local r=false
local t=script.Parent
Spawn(function()
local c=t:Clone()
wait(1)
t.Humanoid.Died:wait()
wait(3)
c.Parent=workspace
c:MakeJoints()
t:Destroy()
end)
local cf={CFrame.new(.8,.5,.4)*CFrame.Angles(math.rad(270),math.rad(40),0),CFrame.new(-1.2,0.5,0.4)*CFrame.Angles(math.rad(270),math.rad(-5),0)}
local c=RbxUtility.Create
local b1=c'Part'{TopSurface=0;BottomSurface=0;Anchored=true;CanCollide=false;BrickColor=BrickColor.White();Size=Vector3.new(.2,.2,1);c'SpecialMesh'{MeshType=6;Scale=Vector3.new(.2,.2,1)}}
local b2=b1:Clone()
b1.Name,b2.Name='1','2'
local mov=c'Part'{Transparency=1;CanCollide=false;Parent=t;Anchored=true}
local h=t:WaitForChild'Handle'
local function reload(boo)
if(boo and ammo~=30)or ammo==0 then
r=true
if w then
w=.03
end
h.Reload:Play()
wait(3)
ammo=30
if w then
w=.14
end
r=false
elseif boo then
wait(.1)
end
end
local function near()
local dis,pl=300,nil
for _,v in ipairs(game.Players:GetPlayers())do
if v.Character and v.Character:FindFirstChild'Humanoid'and v:DistanceFromCharacter(h.Position)<dis then
dis,pl=v:DistanceFromCharacter(h.Position),v
end
end
if pl then
return pl.Character:GetModelCFrame(),dis,CFrame.new(pl.Character.Humanoid.WalkToPoint).lookVector
else
return nil
end
end
Spawn(function()
while true do
t.Humanoid.Jump=true
while not w do wait(.1)end
for i=1,math.random(4,9)do
wait(w)
end
end
end)
Spawn(function()
while wait(math.random(2,7)/10)do
local n=near()
if n and math.random(1,3)==2 or not n then
mov.CFrame=t.Torso.CFrame+t.Torso.CFrame.lookVector*Vector3.new(math.random(-25,25),0,math.random(-25,25))
else
mov.CFrame=n+Vector3.new((math.random(1,2)==1 and 1 or -1)*math.random(25,50),0,(math.random(1,2)==1 and 1 or -1)*math.random(25,50))
end
wait(.1)
t.Humanoid:MoveTo(mov.Position,mov)
end
end)
while true do
local n,mag,hum=near()
if n then
n=n.p
if not r then
w=.14
local dir=h.Position-n
local tst,_=workspace:FindPartOnRay(Ray.new(t.Torso.Position,(n-t.Torso.Position).Unit*999),t)
local aim=n+Vector3.new(math.random(-mag*.015,mag*.015),math.random(-mag*.015,mag*.015),math.random(-mag*.015,mag*.015))*(2+math.random())+hum
if tst and tst.Parent and game.Players:GetPlayerFromCharacter(tst.Parent)then
wait(.08)
h.Fire:Play()
local par,pos=workspace:FindPartOnRay(Ray.new(h.Position,(aim-h.Position).Unit*9999),t)
print(tst)
if par then
local h=par.Parent:FindFirstChild'Humanoid'or par.Parent.Parent:FindFirstChild'Humanoid'
if h and h.Health-10>0 then
h.Health=h.Health-10
elseif h then
h.Health=0
h.Parent:BreakJoints()
end
end
ammo=ammo-1
mag=(h.Position-pos).magnitude
b1.Parent,b2.Parent=t,t
b1.CFrame,b2.CFrame=CFrame.new(h.Position:Lerp(pos,.375),pos),CFrame.new(pos:Lerp(h.Position,.125),h.Position)
b1.Mesh.Scale,b2.Mesh.Scale=Vector3.new(.2,.2,mag*.75),Vector3.new(.2,.2,mag/4)
Delay(.03,function()b1.Parent=nil wait(.03)b2.Parent=nil end)
end
wait(.05)
if ammo==0 then
reload()
end
end
else
reload(true)
w=false
end
end
unrelated but your code is horrendous
anyways I think you can just make a tag and tag all the stuff you want to ignore, then make a ray param and add the tagged instances in the ray params, no need to change much of your code
Your player’s gun uses a raycast that ignores parts. This makes the NPC’s bullets sometimes miss the player.
Maybe focus on being helpful instead of being rude. The code isn’t even bad, it’s just messy.
My goal is to only change the npc without touching the player’s gun scripts, not sure if that is possible but editing 3000 lines of code doesn’t sound like an easy task.
I don’t think the issue is with the player’s gun script.
The issue is likely with the NPC’s gun.
Find out why the NPC’s gun isn’t hitting the player. It’s probably set up to hit the player’s gun or bullets which would block the ray from hitting the player.
Just add the player’s gun and bullets to the ignore list for the NPC’s gun.
Thanks, turns out the source of this problem was GetModelCFrame() and I all had to do is to use GetPrimaryPartCFrame() instead. I was surprised when I just edited the 50th line and it worked because not a long time ago I thought I had to modify the tool’s scripts
local RbxUtility = require(game:GetService("ReplicatedStorage"):
WaitForChild("LoadLibrary"):WaitForChild("RbxUtility"))
wait(.1)
local ammo=29
local w=.14
local r=false
local t=script.Parent
Spawn(function()
local c=t:Clone()
wait(1)
t.Humanoid.Died:wait()
wait(3)
c.Parent=workspace
c:MakeJoints()
t:Destroy()
end)
local cf={CFrame.new(.8,.5,.4)*CFrame.Angles(math.rad(270),math.rad(40),0),CFrame.new(-1.2,0.5,0.4)*CFrame.Angles(math.rad(270),math.rad(-5),0)}
local c=RbxUtility.Create
local b1=c'Part'{TopSurface=0;BottomSurface=0;Anchored=true;CanCollide=false;BrickColor=BrickColor.White();Size=Vector3.new(.2,.2,1);c'SpecialMesh'{MeshType=6;Scale=Vector3.new(.2,.2,1)}}
local b2=b1:Clone()
b1.Name,b2.Name='1','2'
local mov=c'Part'{Transparency=1;CanCollide=false;Parent=t;Anchored=true}
local h=t:WaitForChild'Handle'
local function reload(boo)
if(boo and ammo~=30)or ammo==0 then
r=true
if w then
w=.03
end
h.Reload:Play()
wait(3)
ammo=30
if w then
w=.14
end
r=false
elseif boo then
wait(.1)
end
end
local function near()
local dis,pl=300,nil
for _,v in ipairs(game.Players:GetPlayers())do
if v.Character and v.Character:FindFirstChild'Humanoid'and v:DistanceFromCharacter(h.Position)<dis then
dis,pl=v:DistanceFromCharacter(h.Position),v
end
end
if pl then
return pl.Character:GetPrimaryPartCFrame(),dis,CFrame.new(pl.Character.Humanoid.WalkToPoint).lookVector --GetPrimaryPartCFrame() instead of GetModelCFrame()
else
return nil
end
end
Spawn(function()
while true do
t.Humanoid.Jump=true
while not w do wait(.2)end
for i=1,math.random(4,9)do
wait(w)
end
end
end)
Spawn(function()
while wait(math.random(2,7)/10)do
local n=near()
if n and math.random(1,3)==2 or not n then
mov.CFrame=t.Torso.CFrame+t.Torso.CFrame.lookVector*Vector3.new(math.random(-25,25),0,math.random(-25,25))
else
mov.CFrame=n+Vector3.new((math.random(1,2)==1 and 1 or -1)*math.random(25,50),0,(math.random(1,2)==1 and 1 or -1)*math.random(25,50))
end
wait(.1)
t.Humanoid:MoveTo(mov.Position,mov)
end
end)
while true do
--n=CFrame of character within listd distance of NPC
--mag=the distance limit
--hum=look vector that looks at where the player character was when "near"
local n,mag,hum = near()--calls near function and that functions sets n,mag,hum
if n then --if a character was near, and CFrame was sent
n=n.p--takes position from cFrame without rotational info
if not r then --r is reload debounce, and false means not reloading at moment
---
-- Build a "RaycastParams" object
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {t}--probably t
raycastParams.IgnoreWater = true
raycastParams.RespectCanCollide = true
---
w=.0001 --w as wait time is reset (this is global)
local dir=h.Position-n--h is NPC gun, n is character cframe
local rayCastTest = workspace:Raycast(t.Torso.Position,(n-t.Torso.Position).Unit*99999, raycastParams)
local aim=n+Vector3.new(math.random(-mag*.015,mag*.015),math.random(-mag*.015,mag*.015),math.random(-mag*.1,mag*.1))*(2+math.random())+hum --this decides where bullet lands with some randomness
if rayCastTest and rayCastTest.Instance and rayCastTest.Instance.Parent and game.Players:GetPlayerFromCharacter(rayCastTest.Instance.Parent)then--checks to see if part that was hit by ray was the character's part
wait(0.1)-- between shots?
h.Fire:Play()--plays gunsound from gun
local rayCastReal = workspace:Raycast(h.Position,(aim-h.Position).Unit*99999, raycastParams)
if rayCastReal and rayCastReal.Instance then--raycast was succesful
local h=rayCastReal.Instance.Parent:FindFirstChild'Humanoid'or rayCastReal.Instance.Parent.Parent:FindFirstChild'Humanoid'--h is humanoid of character hit
if h and h.Health-10>0 then--do damage if character won't die
h.Health=h.Health-10
elseif h then--character has less health than damage so kill it
h.Health=0
h.Parent:BreakJoints()
end
end
if rayCastReal and rayCastReal.Instance then
ammo=ammo-1--reduce ammo for shot
mag=(h.Position-rayCastReal.Instance.Position).magnitude--distance between character humanoid and NPC (t)
b1.Parent,b2.Parent=t,t--parents bullets to npc
b1.CFrame,b2.CFrame=CFrame.new(h.Position:Lerp(rayCastReal.Instance.Position,.375),rayCastReal.Instance.Position),CFrame.new(rayCastReal.Instance.Position:Lerp(h.Position,.125),h.Position)--lerps bullets to character
b1.Mesh.Scale,b2.Mesh.Scale=Vector3.new(.2,.2,mag*.75),Vector3.new(.2,.2,mag/4)--changes appearance of bullets?
delay(.03,function()b1.Parent=nil wait(.03)b2.Parent=nil end)--removes bullets
end
end
wait(.05)
if ammo==0 then
reload()--calls reload but doesn't pass "true" ?? so is then "false" and doesn't reload?
end
end
else
reload(true) --whys it call reload if no "n" cframe?
w=false --why is w now a boolean?
end
end