Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

GUI clicked (x) amount of times, (x) amount of duplicates given instead of one?

Hello! I am having an issue with a selection gui. When you select what you want multiple times and click submit, it should give you one item in your inventory, however it gives you many of them.

Basically I have a LocalScript, and in it there is a value gets changed whenever you select a different animal. Then when you finalize your choice, it fires a remote telling the server to give you the item in your inventory, here being the problem as it gives you as many as you clicked.

I have tried making it change a name of an object, but it still gives me multiple for some reason.

Here is some code if you would like to see:

~LocalScript that triggers remote

local folder = game.Players.LocalPlayer:WaitForChild("Animals")

local event = game:GetService("ReplicatedStorage").Events.AnimalConfig.animalpick

local animalchosen = script.Parent.animalchosen

local confirmation = script.Parent.Parent.Parent.Confirmation

local isSelected = false

for i,v in pairs (script.Parent:GetDescendants()) do
	if v:IsA("ImageButton") then
		
		--When button clicked
		v.MouseButton1Click:Connect(function()
			
			if animalchosen.Name == v.Parent.Parent.Parent.Name then
				isSelected = true
				
			elseif animalchosen.Name ~= v.Parent.Parent.Parent.Name then
				isSelected = false
			animalchosen.Name = v.Parent.Parent.Parent.Name
			if animalchosen then
				confirmation.Visible = true
				confirmation.MouseButton1Click:Connect(function()
					local animalchosenNAME = animalchosen.Name
					event:FireServer(animalchosenNAME)
					
					script.Parent.Parent.Parent.Enabled = false
						
					end)
					
				end
			end
		end)
	end
end

~ServerScript where remote is fired

game.ReplicatedStorage.Events.AnimalConfig.deleteanimal.OnServerEvent:Connect(function(player, deleting)
	
	--give player half money back from animal
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + deleting.Cost.Value/2
	deleting:Destroy()
end)

local Event = game:GetService("ReplicatedStorage").Events.AnimalConfig.animalpick

Event.OnServerEvent:Connect(function(player,v)
	print(v)
end)

game.ReplicatedStorage.Events.AnimalConfig.animalpick.OnServerEvent:Connect(function(player, animalchosenNAME)
	print(animalchosenNAME)
	
	--Finds which animal it is
	local animal = game.ReplicatedStorage.Characters:FindFirstChild(animalchosenNAME)
	
	--Clones animal to player inventory
	local newanimal = animal:Clone()
	newanimal.Parent = player.Animals
	
end)

If anyone knows how to fix my issue or any methods around this that would be great. I apologize if this seems confusing. Thank you for reading. :)

1 comment

0

Include code please.

1 answer

✓ Accepted answer

The error lies in your for loop where you're connecting the events.

Line 25

Everytime they click the ImageLabel, another event listener is being attached to the confirm button. So, they press the ImageLabel 10 times, there's 10 event listeners waiting for the confirmation button to be clicked.

Solution

In your for loop, add a single listener to each confirmation button. Don't make them visible until the ImageLabel is clicked, and then you can make the confirmation button visible.


If this helped, don't forget to mark it as an answer!

2 comments

0

Thank you so much! I somewhat understand but will have to read up more on that :)

0

No problem! Let me know if you need a better explanation.

This discussion is preserved as it was. New answers, comments, and votes are closed.