Need help with this humanoid issue

Hi, so this script is for a ai bot but the issue is, when you kill the bot it kills ALL players and not just the bot… and I cant find out why. This is the script:

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=require(game.ReplicatedStorage:WaitForChild(“LoadLibrary”):WaitForChild(“RbxUtility”)).Create
local b1=c’Part’{TopSurface=0;BottomSurface=0;Anchored=true;CanCollide=false;BrickColor=BrickColor.new(“Really red”);Size=Vector3.new(.2,.2,1);c’SpecialMesh’{MeshType=6;Scale=Vector3.new(.2,.2,1)}};Transparency=.5
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
999),t)
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

Sorry it’s so long. Any tips or ideas on what could be causing this would be SUPER helpful.

1 Like

Quick question if t = script.Parent then where is the weapon located?

The weapon is under the part “Handle”.