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

Why does this award multiple items?

debounce = false

script.Parent.MouseEnter:connect(function(enter)
	script.Parent.BorderSizePixel = 3
	script.Parent.MouseButton1Click:connect(function(onClick)
		for i, player in pairs(game.Players:GetChildren()) do
			if player.Points.Value >= 100 then
				player.Points.Value = player.Points.Value - 100
				if debounce == false then
					debounce = true
					local ks = game:GetService("ReplicatedStorage").Swords.KnightsSword
					ks:Clone().Parent = player.Backpack
					ks:Clone().Parent = player.StarterGear
					debounce = false
				end
			end
		end
	end)
end)

I have tried many different ways to stop this but nothing works. I have it set to if the player has 100 or more points then give them an item, but it dosent just give them the item and takes 100 away, sometimes it gives you 2 or 3 of the item and charging 200, or 300. Is there a way to prevent that so it the sword is only awarded ONCE and they cant pruchase another for 10 seconds.

1 comment

1

Why are you getting a list of players in a button event? This is not necessary.

3 answers

✓ Accepted answer

Your problem is that you are connecting the MouseButton1Click event inside of the MouseEnter event.

Each 'instance' of this MouseButton1Click event shares the same 'debounce', but because they all fire at almost exact same time, multiple events can still get through.

To fix it, just separate the two.

Also, you seem to only want the person who clicked the GUI to get the item, yet you looped through every player, so I'm going to change your code to only work on the person who clicked.

Lastly, you never actually checked the debounce value to see if it was true or false, and since MouseButton1Click doesn't 'bounce fire' it's unnecessary anyhow.

player = game.Players.LocalPlayer
 local ks = game:GetService("ReplicatedStorage").Swords.KnightsSword

script.Parent.MouseEnter:connect(function(enter)
    script.Parent.BorderSizePixel = 3
end)

script.Parent.MouseButton1Click:connect(function(onClick)
	if player.Points.Value >= 100 then
		player.Points.Value = player.Points.Value - 100
		ks:Clone().Parent = player.Backpack
		ks:Clone().Parent = player.StarterGear
	end
end)

1 comment

0

Thanks :)

debounce = false

script.Parent.MouseEnter:connect(function(enter)
    script.Parent.BorderSizePixel = 3
    script.Parent.MouseButton1Click:connect(function(onClick)
        for i, player in pairs(game.Players:GetChildren()) do
            if player.Points.Value >= 100 then
                player.Points.Value = player.Points.Value - 100
                if debounce == false then
                    debounce = true
                    local ks = game:GetService("ReplicatedStorage").Swords.KnightsSword
                    ks:Clone().Parent = player.Backpack
                    ks:Clone().Parent = player.StarterGear
                    wait(10) -- No point in the debounce if you don't add a wait (it goes straight from true to false)
                    debounce = false
                end
            end
        end
    end)
end)

1 comment

0

Ive tried the wait, that stops them from buying for 10 seconds, but thats not really what I want, I want only 1 item awarded, even with or without wait they award 2-3 items

debounce = false
-- I would recommend to put another value inside the player?
 
script.Parent.MouseEnter:connect(function(enter)
    script.Parent.BorderSizePixel = 3
    script.Parent.MouseButton1Click:connect(function(onClick)
        for i, player in pairs(game.Players:GetChildren()) do
            if player.Points.Value >= 100 and player.Awarded.Value then -- Awarded is the value that prevents points to be awarded to the player two times. Just make a new value inside the player, and make it a bool value.
                player.Points.Value = player.Points.Value - 100
		player.Awarded.Value = false
                if debounce == false then
                    debounce = true
                    local ks = game:GetService("ReplicatedStorage").Swords.KnightsSword
                    ks:Clone().Parent = player.Backpack
                    ks:Clone().Parent = player.StarterGear
                    debounce = false
                end
            end
        end
    end)
end)

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