Hi there.
I was trying to make a NPC system, in a way that if the NPC is in a specific radius of a player, it follows the player, but only if the player is within the NPC’s boundaries, and when the NPC is killed, a new one respawns after 10 seconds. The problem lies here: After the 10 seconds is up, my studio crashes. Here is what I mean.
ZvGGlO8g9n - YouTube
RBXL file:
NPCfile.rbxl (45.9 KB)
local pathfindingservice = game:GetService("PathfindingService")
local CollectionService = game:GetService("CollectionService")
local PhysicsService = game:GetService("PhysicsService")
local TeleportModule = require(game.ReplicatedStorage.Teleport)
local Levels = {
FIVE = 100,
TEN = 200,
FIFTEEN = 250,
TWENTY = 400,
TWENTYFIVE = 500
}
function onDied(v, Level)
wait(10)
print(Level)
local ClonedCopy = game.ReplicatedStorage.Rig:Clone()
local Location = v:FindFirstChild("Center").Location.Value
ClonedCopy.Center.Location.Value = Location
ClonedCopy.Center.Value = v.Center.Value
ClonedCopy.Level.Value = Level
CollectionService:RemoveTag(v, "Rigs")
v:Destroy()
ClonedCopy.Humanoid.MaxHealth = Levels[Level]
ClonedCopy.Humanoid.Health = Levels[Level]
local TELEPORT_DESTINATION = Location.Position
local FREEZE_CHARACTER = true
ClonedCopy.Parent = workspace
local TELEPORT_FACE_ANGLE = 0
local params = {
destination = TELEPORT_DESTINATION,
faceAngle = TELEPORT_FACE_ANGLE,
freeze = FREEZE_CHARACTER
}
TeleportModule.Teleport(ClonedCopy.Humanoid, params)
CollectionService:AddTag(ClonedCopy, "Rigs")
end
CollectionService:GetInstanceAddedSignal("Rigs"):Connect(function(NewInstance)
print("New instance")
NewInstance.Humanoid.Died:Connect(function()
onDied(NewInstance, NewInstance.Level.Value)
end)
end)
CollectionService:GetInstanceRemovedSignal("Rigs"):Connect(function(NewInstance)
print("NewDELEED")
end)
for i, v in pairs(game.Workspace:GetChildren()) do
if v:IsA("Folder") and string.find(v.Name, "Island") then
local IslandPart = v:FindFirstChild("Island")
if IslandPart then
CollectionService:AddTag(IslandPart, "Islands")
end
end
end
local function GetClosestPlayer(Range, Position)
local PlayerDist
local player, distance = nil, math.huge
for i, Player in pairs(game.Players:GetPlayers()) do
local char = Player.Character or Player.CharacterAdded:Wait()
if char and char:FindFirstChildOfClass("Humanoid") and char:FindFirstChild("HumanoidRootPart") then
local PlayerPos = char:FindFirstChild("HumanoidRootPart").Position
PlayerDist = (PlayerPos - Position).Magnitude
if PlayerDist < distance and PlayerDist <= Range then
player, distance = Player, PlayerDist
end
end
end
return player, distance
end
while true do
wait()
for i, v in pairs(CollectionService:GetTagged('Islands')) do -- island parart
player, distance = GetClosestPlayer(50, v.Position)
if player ~= nil then
AllNPCS = v.Parent
print(AllNPCS)
end
end
if AllNPCS ~= nil then
for i, v in pairs(AllNPCS:GetChildren()) do
if v.Name == "Dummy" then
print(CollectionService:GetTagged("Rigs"))
CollectionService:AddTag(v, "Rigs")
local Center = v:FindFirstChildOfClass('ObjectValue')
local Location = Center:FindFirstChildOfClass("ObjectValue")
player, distance = GetClosestPlayer(15, v.HumanoidRootPart.Position)
if player ~= nil and Center.Value and Location.Value and (player.Character.HumanoidRootPart.Position - Center.Value.Position).Magnitude < 50 then
v.Humanoid:MoveTo(player.Character.HumanoidRootPart.Position)
elseif player ~= nil and (player.Character.HumanoidRootPart.Position - Center.Value.Position).Magnitude > 50 then
v.Humanoid:MoveTo(Location.Value.Position)
end
end
end
end
end
Any help would be appreciated.