HttpService Help-Urgent

I made this script that logs the players money using for loops, but i want it to send the request once, it sends it multiple times at once. here’s the code, I would love to get some help.

local http = game:GetService("HttpService")
local webhook = "not telling"

game.Players.PlayerAdded:Connect(function(plr)
    print("player joined")
	local disc = plr:WaitForChild("Discord")
	local lead = plr:WaitForChild("leaderstats")
	
	local rod = lead:WaitForChild("RobloxianDollars")
	local uuid = disc:WaitForChild("UUID")
	
	while true do
		print("waiting for plr")
		wait(10)
		rod:GetPropertyChangedSignal("Value"):Connect(function()
			print("money value changed sending request")
			local data = {
				["content"] = "<@" .. uuid.Value .. ">" .. " has " .. rod.Value .. " Robloxian Dollars"
			}
			local fdata = http:JSONEncode(data)
			http:PostAsync(webhook,fdata,Enum.HttpContentType.ApplicationJson)
		end)
	end
end)

This is pretty urgent aswell.

1 Like

You’re connecting the function in a while loop. Every ten seconds the amount of times it runs that
function increases by one.

3 Likes

Why are you putting an event in a while loop? That’s called a memory leak. I read the script multiple times and I see NO reason for it to be in any kind of loop. Simply put it outside. As FloofyNezzled said every time the loop runs the event increases by one so that means it will run that many requests because of that many events. There is a golden rule in programming that says to avoid if statements, while, for loops or any kind of iterating operation. If you can implement it without those it’s much better, if you can’t without it then do it, but again there is no reason for you to do that in a while true loop.
Also a tip I can give to you, instead of having the content be the whole message you are sending just make the content the ONLY data you need like that uuid or rod.value, remove that text instead and make it on the other server. Also try to use compression, it might help you reduce the string sizes and overall traffic.

2 Likes

is a while loop by itself enough?

You probably didn’t understand, so let me help you: REMOVE THE :face_with_symbols_over_mouth: WHILE LOOP

Explanation

For some reason you are connecting the event multiple times to the GetPropertyChangedSignal("Value") Event, and that means that whenever Value changes, all of the code inside it will be called the same amount of times it was connected (once every ten seconds, add it all up…)

Also, you could store the webhook in a Secret

The point is that you don’t need a while loop

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