Money Drop script spamming

I have a script where you can say /drop {amount} to drop that number of ‘Moneys’, and it works. However, it sometimes spams error messages in chat, for example if i say ‘/drop 5’ and it works but then it says ‘Please wait 5s before dropping again!’ like 5 times or something and idk why.

Script:

local coin = game.ServerStorage:WaitForChild("PlayerDropMoney")
local dropCooldown = 5
local lastDropTimes = {}
local maxDropAmount = 1000

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		plr.Chatted:Connect(function(msg)
			if string.lower(msg):match("/drop") then
				local currentTime = tick()
				local lastDropTime = lastDropTimes[plr]

				if lastDropTime and currentTime - lastDropTime < dropCooldown then
					local remainingTime = dropCooldown - (currentTime - lastDropTime)
					game.ReplicatedStorage.Chat:FireClient(plr, "You must wait "..math.ceil(remainingTime).."s before dropping again!", Color3.fromRGB(255, 100, 100))
					return
				end

				msg = string.gsub(msg, "/drop", "")
				local amt = tonumber(msg)

				if amt ~= nil and math.floor(amt) == amt and amt > 0 and amt <= maxDropAmount then
					local moneys = plr.leaderstats:FindFirstChild("Moneys")

					if moneys and amt <= moneys.Value then
						moneys.Value = moneys.Value - amt
						local clone = coin:Clone()
						clone.Ui.Label.Text = tostring(amt)
						clone.Ui.From.Text = plr.Name
						clone.Amount.Value = amt
						clone.Position = char.HumanoidRootPart.Position + char.HumanoidRootPart.CFrame.LookVector * 5
						clone.Parent = workspace
						game.ReplicatedStorage.Chat:FireClient(plr, "Successfully dropped "..amt.." Moneys", Color3.fromRGB(100, 255, 100))
						game.ReplicatedStorage.MoneySound:FireClient(plr, 1)

						lastDropTimes[plr] = currentTime -- Update last drop time
					else
						game.ReplicatedStorage.Chat:FireClient(plr, "Invalid amount", Color3.fromRGB(255, 100, 100))
						game.ReplicatedStorage.MoneySound:FireClient(plr, 3)
					end
				else
					game.ReplicatedStorage.Chat:FireClient(plr, "Please specify a valid integer amount (1-"..maxDropAmount..")", Color3.fromRGB(255, 100, 100))
				end
			end
		end)
	end)
end)

It’s because you are creating an event inside of an event. Every time the player respawns, a new event listener is created which will make it stack.

You can either disconnect the old chatted event when their character is added, or just have the player.Chatted event inside of players.PlayerAdded and look for their character in the player’s Character property.

1 Like

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