Sorry for the long title, its a very specific thing. As you can see from the title I want to make a sort of crafting script so that when a player has 3 of the same item in their backpack and clicks a part, then that part checks if there are 3 or more of that item and if there is then delete that 3 items and grant them another item. I have tried several different ways to do this but I always get errors.
Here is a script that I made earlier, I get an error every time saying that it was expecting a table and got a number. I am quite new to scripting so I don’t quite get how to fix that.
local ClickDetector = script.Parent:WaitForChild("ClickDetector")
local shardlist = {}
ClickDetector.MouseClick:Connect(function(player)
for shardlist,instance in pairs(player.Backpack:GetDescendants()) do
if instance:IsA('Tool') then
if instance.Name == "Exotic Shard" then
if table.find(shardlist, Instance.Name) >= 3 then
instance:Destroy(3)
game.ReplicatedStorage.Tools:WaitForChild('Exotic Arrow'):Clone().Parent = player:WaitForChild('Backpack')
end
else
warn("Player does not have shards")
end
end
end
end)
Just with basic knowledge, I think the reason you are receiving errors is due to the fact you are using table.find() function on a number as stated previously. You should probably use something like table.count() or another method instead.
local shardlist = {}
ClickDetector.MouseClick:Connect(function(player)
for _,instance in pairs(player.Backpack:GetDescendants()) do
if instance:IsA('Tool') and instance.Name == "Exotic Shard" then
shardlist[instance.Name] = (shardlist[instance.Name] or 0) + 1
end
end
if shardlist["Exotic Shard"] >= 3 then
for _,instance in pairs(player.Backpack:GetDescendants()) do
if instance:IsA('Tool') and instance.Name == "Exotic Shard" then
instance:Destroy()
end
end
game.ReplicatedStorage.Tools:WaitForChild('Exotic Arrow'):Clone().Parent = player:WaitForChild('Backpack')
else
warn("Player does not have enough shards")
end
end)
I have 0 clue if this will work without more knowledge on the script’s performance and I just wrote it in the chat editor, Let me know if there’s anything else.
I tried it and it works with 3 but I tried only putting in 1 instead of 3 items and it gave me the output item so somehow it isn’t counting and its just giving the output item no matter how many items you put into it.
I see, it may be because it’s not counting exactly how many have been used & should rather keep count.
local ClickDetector = script.Parent:WaitForChild("ClickDetector")
local shardCount = 0
ClickDetector.MouseClick:Connect(function(player)
for _,instance in pairs(player.Backpack:GetDescendants()) do
if instance:IsA('Tool') and instance.Name == "Exotic Shard" then
shardCount = shardCount + 1
end
end
if shardCount >= 3 then
for _,instance in pairs(player.Backpack:GetDescendants()) do
if instance:IsA('Tool') and instance.Name == "Exotic Shard" then
instance:Destroy()
end
end
game.ReplicatedStorage.Tools:WaitForChild('Exotic Arrow'):Clone().Parent = player:WaitForChild('Backpack')
shardCount = 0
else
warn("Player does not have enough shards")
end
end)
I apologize but I don’t really have any clue on what’s going on at all so this may or may not work.
local __ITEMS = {}
ClickDetector.MouseClick:Connect(function(player: Player?)
local __HUMANOID = player.Character:FindFirstChildOfClass("Humanoid")
for __, __BACKPACK in player.Backpack:GetDescendants() do
if __BACKPACK:IsA("Tool") then -- Obviously..
if __BACKPACK.Name == "Exotic Shard" then
table.insert(__ITEMS, __BACKPACK)
end
end
end
if #__ITEMS >= 3 then
for __, __BP2 in player.Backpack:GetDescendants() do
if __BP2:IsA("Tool") then -- Obviously..
if __BP2.Name == "Exotic Shard" then
__HUMANOID:UnequipTools() -- To prevent any bugs from occuring because tools are a funny instance.
__BP2:Destroy()
--Fire Event
end
end
end
else
warn()
end
end)
A cleaner version.
Make sure to clear the table after you’re done to save performance!