Pcall not returning error but yielding script

im trying to make a simple data saving function though it stops at the pcall. when printing statements the script will print “repeating” but will not advance to print “finished pcall”. I think that the pcall is holding up the script but i have no clue why or how to fix the issue

image

ive tried looking on the devforum but the pcall does not yield any errors but instead yields the script infinitely

Edit: the problem is the :SetAsync() function of my datastore that causes an infinite yield

have you tried just doing local success, message = pcall, instead of defining both sucess and message first? im not familiar with pcalls, but i think you do it all in one line, like this

local success, errorMessage = pcall(function()
--code
end)
local function safecall(ac, f, ...)
  local s,e,try = nil,nil,1
  while try < ac+1 do
      s,e = pcall(f, ...)
      if s then
        break
      end
      warn(e)
      try = try + 1
      task.wait(--[[enter wait time here]])
  end
end

--example usage
safecall(1, function()
  
end)

I tried to remake the function tell me if this one works

This function should work the same as your current one, but without the yielding issue:

local function save_player_data(player, attempts)
	local attempts = attempts or 5

	if sessionData[player.UserId] then
		local success, message = pcall(playerDataStore.SetAsync, playerDataStore, player.UserId, sessionData[player.UserId])

		if success then
			print("Data saved for "..player.Name)
		else
			if attempts > 0 then
				task.wait(3)

				save_player_data(player, attempts - 1)
			else
				warn("Unable to save for "..player.Name)
			end
		end
	end
end

You can optionally add a number value as the second argument if you decide to change the number of attempts in the future

1 Like

by defining success locally in the repeat - until loop the if statement that checks for success does not know what “success” refers to

i replaced my previous code for this but it didnt get past the pcall as it didnt print anything after

It could be the case that Studio’s closing the server too quickly for the function to finish working, so try adding this at the bottom of the script:

if game:GetService("RunService"):IsStudio() then
	game:BindToClose(function()
		task.wait(2)
	end)
end

image
implementing “BindToClose()” event in itself seems to have solved the issue though i have no clue why, same function and everything

(i decided to save player data in case of a server shutdown for updates or any other unexpected event)

1 Like

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