Trouble with AI Shooting NPC & Skintone Script

Title says it all,

Output:
image

Skintone Script
SKINTONE SOLVED by @sjr04

View Script
local ListOfBrickColorzs={"Brick yellow", "Light orange", "Nougat", "Bright orange"}

local lol = ListOfBrickColorzs[math.random(1,6)]

ChoosenedColor = BrickColor.new(lol)

for i,v in pairs(script.Parent:GetChildren()) do

if not v.Name == "Sound" and not v.Name == "RaceGenerator" and not v.Name == "Sheik" and not v.Name == "Robot" and not v.Name == "Respawn" and not v.Name == "RagdollR15" and not v.Name == "Humanoid" and not v.Name == "GlassesBlackFrame" and not v.Name == "Control" and not v.Name == "Beard" and not v.Name == "Animate" and not v.Name == "Shirt Graphic" and not v.Name == "Shirt" and not v.Name == "Pants" then

v.BrickColor=ChoosenedColor

end
end

Control script for NPC shooting

View script
wait(.1)
local ammo=30 -- How much ammo the Enemy has
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.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) -- How long the Enemy reloads
		ammo=30
		if w then
			w=.14
		end
		r=false
	elseif boo then
		wait(.1)
	end
end
local function near()
	local dis,pl=200,nil -- Range of the Enemy
	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=false
		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.UpperTorso.CFrame+t.UpperTorso.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.UpperTorso.Position,(n-t.UpperTorso.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(.1)
				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-5 -- How much damage the gun inflicts
					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,.3),pos),CFrame.new(pos:Lerp(h.Position,.175),h.Position) -- Gun Accuracy (ONLY EDIT IF YOU KNOW WHAT TO DO)
				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(1) -- How fast the enemy shoots
			if ammo==0 then
				reload()
			end
		end
	else
		reload(true)
		w=false
	end
end
1 Like

The stack trace comes from a plugin. Not related to the given code at all.

Btw…you can simplify that.

for _, child in ipairs(script.Parent:GetChildren()) do
    if child:IsA("BasePart") then
        child.BrickColor = ChoosenedColor
    end
end
1 Like

I"m getting this now.

image

local ListOfBrickColorzs={"Brick yellow", "Light orange", "Nougat", "Bright orange"}
local lol = ListOfBrickColorzs[math.random(1,6)]
ChoosenedColor = BrickColor.new(lol)
for _, child in ipairs(script.Parent:GetChildren()) do
    if child:IsA("BasePart") then
        child.BrickColor = ChoosenedColor
    end
end

Your table only has 4 elements but you’re choosing one from 1 to 6.

It looks like you had 6 before so you hardcoded the length of the table. Don’t do that.

local lol = ListOfBrickColorzs[math.random(#ListOfBrickColorzs)]

Im so dumb, thank you. Now just got the ai shooting, arms falling off, and movement bug :confused: