Making sure this only runs when first player joins

I’m trying to detect if http service is working and if it is, turn a variable into the returned value, if not, do nothing, however I want it to only run once.

local CachedList = nil
local NoNeed = false
local httpService = game:GetService("HttpService")
Players.PlayerAdded:Connect(function(player: Player) 
	if not NoNeed then
		xpcall(function(...) 
			local successful, result = pcall(httpService.GetAsync, httpService, "https://apiurlthing.com/raw/URL")
			if successful then
				print("somehow wokrks")
				local va = httpService:GetAsync("https://apiurlthing.com/raw/URL")
				CachedList = httpService:JSONDecode(va)
				va = nil
			elseif result:lower():find('http requests are not enabled') then
				print("nope")
				workspace:SetAttribute("NoHTTP",true)
			else
				print("nope2")
				warn('Invalid HTTP request:',result)
			end
		end,function(a0) 
			print(a0)
		end)
		NoNeed = true
		if CachedList ~= nil then
			player:SetAttribute("PaidCoins", CachedList[player.Name].PaidCoins)
		end
	end
	if NoNeed then
		if CachedList ~= nil then
			player:SetAttribute("PaidCoins", CachedList[player.Name].PaidCoins)
		end
	end
end)
2 Likes

When the first player joins, the server will be created so you actually only need to run it at the start of a script. No Players.PlayerAdded, instead just run it as soon as the server is created. Nothing before it.

I have a loading screen thats suppose to detect if the http service request was successful or not on client.
I tried doing the check outside playeradded but the if statement to see if cachedlist is nil or not gets voided since :GetAsync() yields.

Bump still looking for help regarding this.

im not sure what you mean?
just run it once when the first player joins?

either remove the playeradded event or switch :Connect to :Once

local CachedList = nil
local NoNeed = false
local httpService = game:GetService("HttpService")
Players.PlayerAdded:Once(function(player: Player) 
	if not NoNeed then
		xpcall(function(...) 
			local successful, result = pcall(httpService.GetAsync, httpService, "https://apiurlthing.com/raw/URL")
			if successful then
				print("somehow wokrks")
				local va = httpService:GetAsync("https://apiurlthing.com/raw/URL")
				CachedList = httpService:JSONDecode(va)
				va = nil
			elseif result:lower():find('http requests are not enabled') then
				print("nope")
				workspace:SetAttribute("NoHTTP",true)
			else
				print("nope2")
				warn('Invalid HTTP request:',result)
			end
		end,function(a0) 
			print(a0)
		end)
		NoNeed = true
		if CachedList ~= nil then
			player:SetAttribute("PaidCoins", CachedList[player.Name].PaidCoins)
		end
	end
	if NoNeed then
		if CachedList ~= nil then
			player:SetAttribute("PaidCoins", CachedList[player.Name].PaidCoins)
		end
	end
end)

making sure the xpcall function only runs once when the first player joins.