In my RPG game there are NPCs that are supposed to drop items. When a player damages a PC, it creates a tag that has the player’s name on it. When the NPC dies, it’s supposed to drop an item that is added to the player’s inventory (using Datastore2). However, my function is faulty. How can I check to see if the name of the tag is the same as a name of a player?
–Services
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ServerStorage = game:GetService(“ServerStorage”)
–Modules
local DataStore2 = require(ServerStorage:WaitForChild(“DataStore2”))
–Locals
local basePlate = workspace:WaitForChild(“Baseplate”)
–Bind to clickDetector.MouseClick
local dropchance = function(dropchance)
print(“dropping”)
local players = game:GetService(“Players”)
local maths = math.random(1,1)
local itemlist = script.Parent:GetChildren()
local items = {}
for i=1,#itemlist do
if itemlist[i].Name == players:GetChildren(itemlist[i].Name) then – I’m having issues with this.
local player = players:GetChildren(itemlist[i].Name)
print(player)
if maths >= 1 then
print(“dropping katana”)
local itemName = script.Parent.Parent.Katana
local inventoryStore = DataStore2("Inventory2", player)
inventoryStore:Update(function(currentTable)
--If entry exists, increment, else create entry
if (currentTable[itemName]) then
currentTable[itemName] = currentTable[itemName] + 1
else
currentTable[itemName] = 1
end
--MUST return updated table
return currentTable
end)
end
end
end
end
script.Parent.Humanoid.Died:Connect(dropchance)