So, i’m making this nextbot game with my AI. The AI works fine in every other game, but is glitching in this one. I’m making it so that the AI spawns under a tombstone, and will tween up using CFrame to the ground off the tombstone to then go after you. Whenever i do this though, this happens:
local Nextbots = {}
function FindClosest(Player)
local Spawns = workspace.Game.Graves
local Closest
local PlayerPosition = Player.Character.PrimaryPart.Position
for i,v in pairs(Spawns:GetChildren()) do
if Closest == nil then
Closest = v
else
if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
Closest = v
end
end
return Closest
end
end
function AILeave(NPC)
local Humanoid: Humanoid = NPC:WaitForChild("Humanoid")
local Graves = workspace.Game.Graves:GetChildren()
local RandomGrave = Graves[math.random(1, #Graves)]
Humanoid:MoveTo(RandomGrave.Ground.Position)
Humanoid.MoveToFinished:Connect(function()
wait(2)
local HRP = NPC:WaitForChild("HumanoidRootPart")
local TS = game:GetService("TweenService")
local Goal = RandomGrave:WaitForChild("AISpawn").CFrame
local TI1 = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local AIDown = TS:Create(HRP, TI1, {CFrame = Goal})
AIDown:Play()
RandomGrave:WaitForChild("AISpawnGoal").Arrival:Play()
AIDown.Completed:Connect(function()
game.ReplicatedStorage.Game.Values.CurrentBots.Value -= 1
NPC:Destroy()
end)
end)
end
function Nextbots.Spawn(CanRandomize)
local Bots = game.ReplicatedStorage.Game.Values.CurrentBots
if Bots.Value <= 0 then
Bots.Value += 1
print("spawning")
local Players = game:GetService("Players"):GetPlayers()
local Player = Players[math.random(1, #Players)]
print(Player.Name)
wait(2)
local ClosestGrave = FindClosest(Player)
local NPC
local Grave = ClosestGrave
local AISpawn = Grave.AISpawn
local GoalSound = Grave.AISpawnGoal.Arrival
local Goal = Grave.AISpawnGoal.CFrame
GoalSound:Play()
if CanRandomize then
NPC = game.ServerStorage.Nextbots.Pinhead:Clone()
else
end
NPC.Parent = workspace
local Head = NPC.Head
local HRP = NPC:WaitForChild("HumanoidRootPart")
HRP.Position = AISpawn.Position
local TS = game:GetService("TweenService")
local TI1 = TweenInfo.new(3, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
local AIUp = TS:Create(HRP, TI1, {CFrame = Goal})
AIUp:Play()
AIUp.Completed:Connect(function()
for _, parts in pairs(NPC:GetChildren()) do
if parts:IsA("Part") then
parts.Anchored = false
end
end
print("npc spawned")
NPC.NextPathHandler.Enabled = true
if HRP:FindFirstChild("audio") then
HRP.audio:Play()
end
end)
wait(10)
NPC.NextPathHandler:Destroy()
AILeave(NPC)
end
end
return Nextbots