Help with giving out too many tools

Hi! So I was working on this script that basically gives you a gun once you buy it.

In this script I have various if-statements that check if the person has a gun permit and enough money.

How do I resolve this issue? I think that because it gives me the tool multiple times, it gets called multilple times, but I wouldn’t know where…

The script:

function sendConfirmation(gun, price, player, legal, realprice)
	if legal == true then
		confirmation.Undertitle.Text = "This gun will be legal, but may only be used in self-defense."
	end
	if legal == false then
		confirmation.Undertitle.Text = "This gun will be illegal, if you are searched you may face prison time."
	end
	confirmation.PriceEtc.Text = "Gun: "..gun..", Price: "..price
	confirmation.Visible = true
	confirmation.Buy.Activated:Connect(function()
		if player.Backpack:FindFirstChild("Gun permit") then
			if player.leaderstats.Cash.Value == realprice or player.leaderstats.Cash.Value > realprice then
				addGun(gun, price, player, legal, realprice)
				closeConfirmation()
			else
				closeConfirmation()
			end
		else
			closeConfirmation()
		end
	end)
	confirmation.Close.Activated:Connect(function()
		closeConfirmation()
	end)
end

Thanks in advance! :smiley:

1 Like

Could you also post the addGun function? the bug might be in there. Also, I found this improvement to your code that you should add:

if player.leaderstats.Cash.Value == realprice or player.leaderstats.Cash.Value > realprice then

should be replaced by

if player.leaderstats.Cash.Value >= realprice then

the >= operator translates to “equals or bigger than”, removing the need for the or statement

Thank you so much, I forgot about that one!

addGun.OnServerEvent:Connect(function(player, gun1)
	local gun = serverStorage[gun1]:Clone()
	gun.Parent = player.Backpack
end)

I can’t find any immediate clues as to what could be the problem, try adding a new line with a print function to the start of the onServerEvent function to see if it is being fired twice or not

Alright so basically once I’ve bought a gun, and then another one it’ll fire twice as much.

oh, it only fires twice when buying the 2nd gun?

Yes, that’s true. It’s kind of weird…

What happens if you buy a 3rd? or a 4th? does it still give it twice?

Yes, sometimes even triple. I have no idea what’s causing this to happen…

Is it not consistent to when it gives it multiple times? Also, has it ever given the gun multiple times on the first purchase?

It never gave the gun multiple times on the first purchase. It’s mostly when you buy the 2nd gun, you get the first gun you bought, when you buy the 3rd gun you get the 1st and 2nd gun bought.

This sounds similar to certain other issues I’ve had where variables weren’t being updated correctly, and since the initial “Gun” variable is from another script I believe that the bug is somewhere else than in the code that you sent here. I could be wrong tough. If you could send where the sendConfirmation function is being called from I might be able to help you further, however I need to leave soon so I need to continue later

It may be because you are connecting the confirmation.Buy.Activated everytime you try to send confirmation. Try using :Once() instead of :Connect()

1 Like

I had a similar issue with a button activated just the other day… I just added a local denounce to work around it.

Thank you so much! I only had to change that small line and it immediately worked!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.