Hi! I have this inventory script that only allows one tool to be equipped at a time. Here is the gui script:
item.SelectButton.MouseButton1Click:Connect(function()
local price = tool.Price.Value
local coins = player.leaderstats.Coins.Value
if item == SelectedTrail then
item.SelectButton.Text = "Equip"
SelectedTrail = nil
else
if player.OwnedTools:FindFirstChild(tool.Name) then
item.SelectButton.Text = "Unequip"
else
item.SelectButton.Text = "Unequip"
end
if SelectedTrail then
SelectedTrail.SelectButton.Text = "Equip"
end
SelectedTrail = item
end
TrailSelectedRE:FireServer(tool)
end)
end
It fires this event which triggers this:
ToolSelectedRE.OnServerEvent:Connect(function(plr, tool)
if not tool or typeof(tool) ~= "Instance" then return end -- we still want this here just in case
local ownedTool = plr.OwnedTools:FindFirstChild(tool.Name)
if not ownedTool then
print(plr.Name," is buying the ",tool.Name," tool.")
local price = tool.Price.Value
local coins = plr.leaderstats.Coins
if price <= coins.Value then
coins.Value -= price
local newTool = tool:Clone() do
newTool.Parent = plr.OwnedTools
end
end
elseif ownedTool then
if plr.Backpack:FindFirstChild(tool.Name) == nil then
if plr.Backpack:FindFirstChildOfClass('Tool') then
plr.Backpack:FindFirstChildOfClass('Tool'):Destroy()
end
print(plr.Name," equipped ",tool.Name," tool.")
local new = tool:Clone()
new.Parent = plr.Backpack
elseif plr.Backpack:FindFirstChild(tool.Name) then
print("triggered")
local deleted = plr.Backpack:WaitForChild(tool.Name)
deleted:Destroy()
end
end
end)
The problem is, if you equip the tool you have in your backpack and then equip another tool, it will bypass the one tool check and the player can access two tools in their inventory. How do I fix this?