Hello.
The only way I see people are using is putting a script inside the NPC to make it when the NPC dies the money goes to the killer. But what if there’s many NPCs and NPCs that are getting cloned ? This will be messy and many scripts. Is there any way to handle this problem in a single script ?
Make a table of NPCs, make sure this stays updated by updating it using the scripts that spawn them, or search. Run a coroutine that loops through the table and adds to a counter when it finds a dead NPC, and also remove the NPC from the table.
Outside of the coroutine or on another script, collect and reset the count, to add the money!
When you add an npc to a folder you can have a ChildAdded event connected to that folder. Doing so will allow you to set up a system in which you can check for tags in the npc from whatever player killed that npc, and award them accordingly.
This is too much, I thought of doing something with tables like this but there should probably be a better way. So collectionService shall be the solution.
I tried something like this and it didn’t work, collectionService is new to me so I did a quick look up into it and understood it clearly but not how to use it clearly yet.
local DataStore2 = require(script.Parent.Server.MainModule)
local CollectionService = game:GetService("CollectionService")
local mobs = workspace.Mobs:GetChildren()
for i,NPC in pairs(CollectionService:GetTagged(mobs)) do
print("Tagged")
NPC.Humanoid.Died:Connect(function()
print("TageDied")
local humanoid = NPC.Humanoid
local tag = humanoid:FindFirstChild("creator")
local killer = tag.Value
print("tag and killerTag")
local players = game.Players:GetPlayerFromCharacter(killer)
local killsStore = DataStore2("Kills", players)
killsStore:Increment(1)
print("+1 Kill!Tag")
end)
end
You’re using CollectionService incorrectly, tags are strings that you give to instances you wish to tag.
Here is what I’ve managed to put together for you, keep in mind this is untested code:
Run this in the Studio Command Bar first:
for _, NPC in pairs(workspace.Mobs:GetChildren()) do
game:GetService("CollectionService"):AddTag(NPC, "Mobs")
end
Main Script:
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
local MOBS_TAG = "Mobs"
local DataStore2 = require(script.Parent.Server.MainModule)
local function onHumanoidDied(humanoid)
humanoid.Died:Once(function()
local tag = humanoid:FindFirstChild("creator")
if tag and tag.Value then
local player = Players:GetPlayerFromCharacter(tag.Value)
if player then
local killsStore = DataStore2("Kills", player)
if killsStore then
killsStore:Increment(1)
end
end
end
end)
end
for i, NPC in pairs(CollectionService:GetTagged(MOBS_TAG)) do
local humanoid = NPC:FindFirstChildWhichIsA("Humanoid")
if humanoid then
onHumanoidDied(humanoid)
end
end
CollectionService:GetInstanceAddedSignal(MOBS_TAG):Connect(function(instance)
local humanoid = instance:FindFirstChildWhichIsA("Humanoid")
if humanoid then
onHumanoidDied(humanoid)
end
end)