I want the script to check if the player has two tools named Banana. Instead, if you’ve one tool named banana it still works. Anyone know how to fix.
local RS = game:GetService("ReplicatedStorage")
local Craft = RS.Events:WaitForChild("Craft")
Craft.OnServerEvent:Connect(function(player)
local Recipes = {
BananaRecipe = {
[1] = "Banana"}
}
local Backpack = player.Backpack
for _, Tool in pairs(Backpack:GetChildren()) do
print("Getting backpack")
if Tool.Name == Recipes.BananaRecipe[1] then
print("has tools")
end
end
end)
instead of looping through the players backpack simply use FindFirstChild. In this case you would switch out your code with the one below.
local RS = game:GetService("ReplicatedStorage")
local Craft = RS.Events:WaitForChild("Craft")
Craft.OnServerEvent:Connect(function(player)
local Recipes = {
BananaRecipe = {
[1] = "Banana"}
}
local Backpack = player.Backpack
if Backpack:FindFirstChild(Recipes.BananaRecipe[1]) then
print("has tools")
end
end)
If he does not have the tool, the player is given only one tool
In this way, the player does not have more than one tool from the same tool
for example
local Part = script.Parent
Part.Touched:Connect(function(hit)
local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
if player then
local backpack = player:FindFirstChild("Backpack")
if backpack then
local RocketLauncher = backpack:FindFirstChild("Pickaxe")
if RocketLauncher == nil then
local NewCharacter = game:GetService("ServerStorage"):WaitForChild("Pickaxe"):Clone()
NewCharacter.Parent = backpack
end
end
end
end)
I would recommend having a dictionary outside the event, also I made it a bit cleaner.
local RS = game:GetService("ReplicatedStorage")
local Craft = RS.Events:WaitForChild("Craft")
local Recipes = {
BananaRecipe = {Requirement = "Banana", Amount = 2} -- Requirement = Toolname, Amount = amount of tools required
}
Craft.OnServerEvent:Connect(function(player)
local Selected = Recipes.BananaRecipe
local Backpack = player.Backpack
local Tools = {}
for _, Tool in pairs(Backpack:GetChildren()) do
if Tool.Name == Selected.Requirement then
if #Tools < Selected.Amount then -- So it doesn't add if there is already enough tools
table.insert(Tools,#Tools+1,Tool) -- Inserts it into the table for later use
end
end
end
if #Tools == Selected.Amount then -- Checks if requirement is met
print("Found tools")-- If you want to do something with the tools they are stored in "Tools" table
end
end)