Hello, I need help, can you tell me how to make it so that the player does not receive more than one of the items called “tool1” “tool2” “tool3” “tool4” in the inventory, for example: Npc gave me one of these items, for example, an item called “tool1” and if the player completed the same quest to obtain one of these items, he cannot obtain other items. Short: If the player got “tool1” then he won’t be able to get “tool2” if he has one of these items I mentioned in his inventory.
You can try checking if the player already has any of those with an if
statement:
local function giveTool(Tool)
-- You can get the player with multiple ways, and you probably already have one defined in the script for giving the player the tool
if not plr.Character:FindFirstChild("tool1") and not plr.Character:FindFirstChild("tool2") and not plr.Character:FindFirstChild("tool3") and not plr.Character:FindFirstChild("tool4") then
--Script here yourself that the player receives the intended tool with the "Tool" parameter
end
end
--Call the function when a player is supposed to receive the tool, also specify the Tool parameter
<Event>.Connect:(giveTool(<Intended Tool>))
I hope that helps with what you’re working on, but if you don’t understand I would ask you to provide the code you currently have (The one that gives the player the tool) so I can understand the problem better.
Thank you for trying to help me, I appreciate it in people, now I’ll try to do as you said, if it doesn’t work, I’ll give you the code. Thank you again :]
I tried your method and unfortunately it didn’t work for me (maybe I did something wrong) I’ll give you my code now. I hope you will help me
local player = game.Players.LocalPlayer
local item = script.Parent
local items = {}
local replicatedstorage = game:GetService("ReplicatedStorage")
local buyevent = replicatedstorage.PurchaseQUEST
local deb = false
local S = 4
item.MouseButton1Click:Connect(function()
for i,v in pairs(player.Backpack:GetChildren()) do
table.insert(items,v.Name)
end
if table.find(items,"The Truth About the Soul") then
player.Backpack["The Truth About the Soul"]:Destroy()
S = math.random(1,4)
if S == 1 then
buyevent:FireServer("Wind Punch")
elseif S == 2 then
buyevent:FireServer("Ice Spike")
elseif S == 3 then
buyevent:FireServer("Flame Thrower")
elseif S == 4 then
buyevent:FireServer("Earth Bullets")
end
end
end)
Presumably that the tools you were talking about before had names: Wind Punch, etc.
Then try this code:
local player = game.Players.LocalPlayer
local item = script.Parent
local items = {}
local replicatedstorage = game:GetService("ReplicatedStorage")
local buyevent = replicatedstorage.PurchaseQUEST
local deb = false
local S = 4
item.MouseButton1Click:Connect(function()
for i,v in pairs(player.Backpack:GetChildren()) do
table.insert(items,v.Name)
end
if table.find(items,"The Truth About the Soul") then
if not table.find(items, "Wind Punch") and not table.find(items, "Ice Spike") and not table.find(items, "Flame Thrower") and not table.find(items, "Earth Bullets") then
player.Backpack["The Truth About the Soul"]:Destroy()
S = math.random(1,4)
if S == 1 then
buyevent:FireServer("Wind Punch")
elseif S == 2 then
buyevent:FireServer("Ice Spike")
elseif S == 3 then
buyevent:FireServer("Flame Thrower")
elseif S == 4 then
buyevent:FireServer("Earth Bullets")
end
end
end
end)
I added an if function which will check if in the items table any of the tools that the player may have then prevents the player from getting another one.
For now, it should work like the player clicks the item of whatever, The Truth About The Soul stays in their inventory, and doesn’t get any more tools.
Let me know if it works.
Yes, everything works, but there is one bug that is not related to this code, but can you help me fix this bug? For example, I will retrieve one of these items and cannot retrieve the next one, but in my game there is an “item storage” in which you can put your item or the command “Give tool” and because of this, you can get these items indefinitely, so you can help me immediately fix this bug, I will be very grateful to you;
You can message me on my profile, and I recommend you mark my reply as a solution so it closes the topic
Thank you for helping me, I will write to you today