Global functions no longer working?

This is probably something that is really simple but I’m just missing it. For some reason my global function is no longer working and being underlined with an error saying: image

Any reason to this or how I can fix it?

game.Players.PlayerAdded:Connect(function(Player)
	local Attempts = 0
	RequestData = function()
		local Success, PlayerStats = pcall(function()
			return PlayerStatsDatastore:GetAsync(Player.UserId)
		end)

		if Success then
			if PlayerStats then
				CreateStats(Player, PlayerStats)
			else
				local PlayerStats = {
					["Tix"] = 100,
					["Level"] = 0,
					["Slots"] = {"typical punch"}
				}
			end
		else
			Attempts += 1
			if Attempts == 3 then
				Player:Kick("Error loading player stats.")
			else
				wait(5)
				RequestData()
			end
		end
	end
	RequestData()
end)

This should work! First you have to make it a variable then assign it to a function if you want to recall the function inside of the same function.

game.Players.PlayerAdded:Connect(function(Player)
	local Attempts = 0
    local RequestData -- Nil variable
	RequestData = function() -- Set function to variable
		local Success, PlayerStats = pcall(function()
			return PlayerStatsDatastore:GetAsync(Player.UserId)
		end)

		if Success then
			if PlayerStats then
				CreateStats(Player, PlayerStats)
			else
				local PlayerStats = {
					["Tix"] = 100,
					["Level"] = 0,
					["Slots"] = {"typical punch"}
				}
			end
		else
			Attempts += 1
			if Attempts == 3 then
				Player:Kick("Error loading player stats.")
			else
				wait(5)
				RequestData() -- now you can call the function from the inside
			end
		end
	end
	RequestData()
end)
1 Like

Just realized I never called the CreateStats function and that fixed it. Thanks for giving me a alternative fix though.

Though in my opinion the script is a bit messy, I suggest making your script look something like this, I have not tested the script so it might have a few flaws though it should work.
You are able to call an function of the GlobalDataStore through it.
e.g. SetAsync, GetAsync

local globalDataStoreCall do
   local MAX_RETRIES = 3 

   local RunService = game:GetService('RunService')

   function globalDataStoreCall(method, key, ...)|
      local results = {}
      for attempt = 1, MAX_RETRIES do
         results = {pcall(PlayerStatsDatastore[method], PlayerStatsDatastore, key, ...)}
         if results[1] then
            break
         end
         RunService.RenderStepped:Wait()
      end
      return results
   end
end

local PlayerService = game:GetService("Players")

PlayerService.PlayerAdded:Connect(function(newPlayer)
   local success, ... = globalDataStoreCall("GetAsync", newPlayer.UserId)
   local playerStats = (success == true and {...} or {Tix = 100, Level = 0, Slots = {"typical punch"}})
   CreateStats(newPlayer, playerStats)
end)

PlayerService.PlayerAdded:Connect(function(oldPlayer)
   local success = globalDataStoreCall("SetAsync", oldPlayer.UserId) -- add what you want to save etc to this
end)

I use the example of this post from 2017 by @Fractality_alt.