Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this award multiple items?

Asked by 10 years ago
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
Why are you getting a list of players in a button event? This is not necessary. Dummiez 360 — 10y

3 answers

Log in to vote
2
Answered by
adark 5487 Badge of Merit Moderation Voter Community Moderator
10 years ago

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)
0
Thanks :) NinjoOnline 1146 — 10y
Ad
Log in to vote
-2
Answered by
SirNoobly 165
10 years ago
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)

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 NinjoOnline 1146 — 10y
Log in to vote
-2
Answered by 10 years ago
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)

Answer this question