Item still going into users inventory

Howdy,

I am trying to make a script where, if the player doesn’t have sufficient money (bucks), the item would not go into their inventory, instead triggering a remote event.
However, the remote event still fires (As a client), but the item still goes into the inventory.

Any help as to why this is happening?

Script
local list = {
	"Bomb";
	"Ball";
}

for _, v in next, click do
	if v:IsA("ClickDetector") then
		v.MouseClick:Connect(function(plr)
			if plr then
				if plr.Character:FindFirstChild("Humanoid") then
					if not table.find(list,v.Name) then
						if not plr.Backpack:FindFirstChild(v.Parent.Name) then
							local clone = storage.Weapons:FindFirstChild(v.Parent.Name):Clone()
							clone.Parent = plr.Backpack
						end
						if plr.leaderstats.Bucks.Value == v.Parent.Value.Value then
							local clone = storage.Weapons:FindFirstChild(v.Parent.Name):Clone()
							clone.Parent = plr.Backpack
						else
							game.ReplicatedStorage.NotEnough:FireClient(plr,plr)
						end
					end
				end
			end
		end)
	end
end

Why the not enough event have 2 plr?

You are cloning and placing the tool in the backpack twice, Despite the player not having enough money.

1 Like

I thought this was needed at first, but instead I changed the script to this:

script
local list = {
	"Bomb";
	"Ball";
}

for _, v in next, click do
	if v:IsA("ClickDetector") then
		v.MouseClick:Connect(function(plr)
			if plr then
				if plr.Character:FindFirstChild("Humanoid") then
					if not table.find(list,v.Name) then
						if plr.leaderstats.Bucks.Value >= v.Parent.Value.Value then
							if not plr.Backpack:FindFirstChild(v.Parent.Name) then
								local clone = storage.Weapons:FindFirstChild(v.Parent.Name):Clone()
								clone.Parent = plr.Backpack
							end
							else
								game.ReplicatedStorage.NotEnough:FireClient(plr,plr)
						end
					end
				end
			end
		end)
	end
end

and it worked. Thanks for helping me see this.

P.s, is this an efficient code?

For such a small amount of code you won’t really have to worry about efficiency, And what you have right now won’t lag your game.

(but please try not to put hard to read variable names, maybe change “v” to “Detector” just for other’s sake.)

1 Like