Hi, I’m making a random weapon box, sometimes the purchase was successful, but other times it says that the purchase failed, but the player has enough cash so the purchase shouldn’t fail.
Script:
game.ReplicatedStorage.BuyRandomGun.OnServerEvent:Connect(function(player)
local items = game.ReplicatedStorage:FindFirstChild("Guns"):GetChildren()
local randomItem = items[math.random(1, #items)]
local NumberOfTools = #player.StarterGear:GetChildren()
if player.leaderstats.Cash.Value >= 70 and not player.StarterGear:FindFirstChild(randomItem.Name) then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -70
local klone = randomItem:Clone()
local klone2 = randomItem:Clone()
klone.Parent = player.Backpack
klone2.Parent = player.StarterGear
game.ReplicatedStorage.SuccessPurchaseBox:FireClient(player)
elseif player.leaderstats.Cash.Value >= 70 and player.StarterGear:FindFirstChild(randomItem.Name) and not NumberOfTools == 6 then
repeat
randomItem = items[math.random(1, #items)]
until not player.StarterGear:FindFirstChild(randomItem.Name)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value -70
local klone = randomItem:Clone()
local klone2 = randomItem:Clone()
klone.Parent = player.Backpack
klone2.Parent = player.StarterGear
game.ReplicatedStorage.SuccessPurchaseBox:FireClient(player)
else
game.ReplicatedStorage.FailedPurchaseBox:FireClient(player)
end
end)