Hello, I am making a part give you money when it touches a Sell part. At the moment, the part gives you money multiple times and sends the server/global message multiple times because it touches multiple times before its destroyed.
I need help with the debounce or whatever is needed to make it only give the money and send the message once.
script.Parent.Touched:Connect(function(hit)
for i, v in pairs(game.ReplicatedStorage.Rarities:GetChildren()) do
if hit.Name == v.Name then
local player = game.Players[script.Parent.Parent.Parent.Name]
local Money = hit.Money.Value
wait(1)
if hit.Name == "Mythical" then
game.ReplicatedStorage.RemoteEvents.Message:FireAllClients(player.Name, hit.Name, "Collector")
end
if hit.Name == "Secret" then
game:GetService("MessagingService"):PublishAsync("GlobalAnnouncement", "[GLOBAL]: "..player.Name.." sold a "..hit.Name.." from ".."Collector".."!")
end
player.leaderstats.Money.Value += Money
hit:Destroy()
end
end
end)
Any and all help is appreciated.
Should just need to initialize a variable before the connection, then inside the connection, make an if statement which checks if that variable is true or false, if false, set to true and run your code.
local debounce = false
script.Parent.Touched:Connect(function(hit)
if not debounce then
debounce = true
for i, v in pairs(game.ReplicatedStorage.Rarities:GetChildren()) do
if hit.Name == v.Name then
local player = game.Players[script.Parent.Parent.Parent.Name]
local Money = hit.Money.Value
wait(1)
if hit.Name == "Mythical" then
game.ReplicatedStorage.RemoteEvents.Message:FireAllClients(player.Name, hit.Name, "Collector")
end
if hit.Name == "Secret" then
game:GetService("MessagingService"):PublishAsync("GlobalAnnouncement", "[GLOBAL]: "..player.Name.." sold a "..hit.Name.." from ".."Collector".."!")
end
player.leaderstats.Money.Value += Money
hit:Destroy()
end
end
debounce = false
end
end)
Question regarding your rarities, the part that the player touches, are there multiple children in the Rarities ReplicatedStorage folder with the same name, or are they unique?
before you put the deboune = false you should put a task.wait(.5) so that some time must pass before it’s able to be used again.
task.wait(.5)
debounce = false