Single NPC generated

  1. What do you want to achieve? Keep it simple and clear!
    I would like to generate one npc on which each of the following functions will be separate for each npc

  2. What is the issue? Include screenshots / videos if possible!
    The problem is that npc when it respawns the code snippet works on the last respawned npc and not on the one you need.

example:

		if npcHasOwner == plr.Name then
			print("npc released by".. plr.DisplayName)
			npcHasOwner = nil
			
		elseif npcHasOwner ~= plr.Name then
			print("npc taken by".. plr.DisplayName)
			npcHasOwner = plr.Name
			
			local BillboardOwner = spawnedNpc.Head:WaitForChild("BillboardOwner")
			BillboardOwner.TextLabel.Text = tostring(plr.Name)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to solve the problem, but I can’t so I decided to write a post here

CODE SCRIPT:

local GameSystem = require(script.GameSystem)

while wait(1) do
	GameSystem.InstanceNpc()
end

CODE MODULE SCRIPT:

-- Services
local RS = game:GetService("ReplicatedStorage")
-- Services

-- VARIABLES
local spawnPads = workspace.NPCSpawnLocations:GetChildren()
local npcFolder = RS.NpcFolder
-- VARIABLES

-- CFG
local NPC_LIMIT = 5
local MAX_DISTANCE = 40
local NPC_INSTANCE_TIME = 1 -- // time in seconds
-- CFG

-- RARITES
local CommonRarity = npcFolder.Common:GetChildren()
local UncommonRarity = npcFolder.Uncommon:GetChildren()
local RareRarity = npcFolder.Rare:GetChildren()
local EpicRarity = npcFolder.Epic:GetChildren()
local LegendaryRarity = npcFolder.Legendary:GetChildren()
-- RARITES

local npcHasOwner = nil

local GameSystem = {}

function GameSystem.RandomChance()
	local randomChance = math.random(7000, 10000)

	print(randomChance)

	if randomChance <= 10000 then
		spawnedNpc = CommonRarity[math.random(1,#CommonRarity)]:Clone()
		spawnedNpc.Parent = workspace.NPCStorage
		spawnedNpc.Name = "Common"
		
		local BillboardRarity = spawnedNpc.Head:WaitForChild("BillboardRarity")
		BillboardRarity.TextLabel.Text = tostring(spawnedNpc.Name)

		local health = math.random(500, 1000)
		spawnedNpc.Humanoid.MaxHealth = health
		spawnedNpc.Humanoid.Health = health

	elseif randomChance <= 50 then
		spawnedNpc = UncommonRarity[math.random(1,#UncommonRarity)]:Clone()
		spawnedNpc.Parent = workspace.NPCStorage
		spawnedNpc.Name = "Uncommon"

		local health = math.random(1000, 3000)
		spawnedNpc.Humanoid.MaxHealth = health
		spawnedNpc.Humanoid.Health = health

	elseif randomChance <= 200 then
		spawnedNpc = RareRarity[math.random(1,#RareRarity)]:Clone()
		spawnedNpc.Parent = workspace.NPCStorage
		spawnedNpc.Name = "Rare"

		local health = math.random(3000, 5000)
		spawnedNpc.Humanoid.MaxHealth = health
		spawnedNpc.Humanoid.Health = health

	elseif randomChance <= 500 then
		spawnedNpc = EpicRarity[math.random(1,#EpicRarity)]:Clone()
		spawnedNpc.Parent = workspace.NPCStorage
		spawnedNpc.Name = "Epic"

		local health = math.random(5000, 10000)
		spawnedNpc.Humanoid.MaxHealth = health
		spawnedNpc.Humanoid.Health = health

	else return "error spawnedNpc"

	end
end

function GameSystem.NpcPlayerHolder()
	
	local clickDetector = spawnedNpc.ClickableHitBox:WaitForChild("ClickDetector")
	clickDetector.MouseClick:Connect(function(plr)
		
		if npcHasOwner == plr.Name then
			print("npc released by".. plr.DisplayName)
			npcHasOwner = nil
			
		elseif npcHasOwner ~= plr.Name then
			print("npc taken by".. plr.DisplayName)
			npcHasOwner = plr.Name
			
			local BillboardOwner = spawnedNpc.Head:WaitForChild("BillboardOwner")
			BillboardOwner.TextLabel.Text = tostring(plr.Name)
			
		else return "error"
		end
	end)
end

function GameSystem.InstanceNpc()
	
	GameSystem.RandomChance()
	GameSystem.NpcPlayerHolder()
	
	if spawnedNpc then
		spawnedNpc.HumanoidRootPart.CFrame = spawnPads[math.random(1,#spawnPads)].CFrame + Vector3.new(0, 5, 0)
	else return "Npc don't find"
	end
	
	return spawnedNpc
end

return GameSystem

2 Likes