Pcall Aborting my ENTIRE SCRIPT

I’m trying to make a datastore using a pcall

my issue is that whenever i call the pcall the entire script aborts

i’ve tried alot of things, i’ve read official docs read free model code, check api access (api access is enabled), i’ve also watch youtube videos

this is the code

local function playerLeft(player)
	if currentPlayersData[player.Name] then
		-- currentplayersdata[player.Name]
		-- this is not the issue
		
		local plrData = currentPlayersData[player.Name]
		local success, errorMsg
		local attempts = maxAttempts
		-- maxattempts = 5

		local data = getSaveData(plrData)
		-- getsavedata returns a table
		
		repeat
			print("before pcall")
			-- code above this line runs
			-- this pcall aborts the script for some reason
			success, errorMsg = pcall(function()
				return databank:SetAsync(getKey(player), data)
			end)
			-- this does not run below this
			print("after pcall")
			attempts -= 1
			if not success then
				warn(errorMsg)
				task.wait(3)
			end
		until success or attempts == 0

		if success then
			print("saved: "..player.Name)
		else
			warn("not saved: "..player.Name)
		end

		print("call end")
	end
end

game.Players.PlayerRemoving:Connect(playerLeft)

also it isnt providing any error, its just stopping

2 Likes

Firstly: Putting “[URGENT]” in your post’s title isn’t going to speed up the process, and just makes you appear impatient.

Second: What is the ‘getKey’ function? It’s possible it has some infinite yielding or something that is preventing the script from functioning

the getkey function is not the issue
this is the get key function

local function getKey(plr)
	return dbKey..plr.UserId
end

its a pretty usless function and its unoptimized but i know that its not the issue
it only returns a string

As SetAsync is non returning, try removing the return part.

I can’t see anything significantly wrong with the code that could be breaking it, so these smaller things should be tested

also the reason i put [URGENT] is not because I’m inpatient
I’m just in a time crunch, I’m not saying my request for help is more important than others, I’m just saying that i need to finish this project before a specific time.

Sorry, I do suppose that it looks like that, i just wanted to be clear

Ohhhhh, you’re right i guess i dont need return oops,

but the issue still persists

it should print out
image

but its only printing
image

The function is yielding most likely, print data and the result of getkey before the setasync.

can you better explain what this means?

do you mean change the getkey function to this?

success, errorMsg = pcall(function()
	databank:SetAsync(dbKey..player.UserId, data)
end)

Are you testing in studio or in game btw? I found a post from a few years back that mentions that it was their studio that was causing the issue.

A thread can be suspended or running. To ensure execution, you can use task.spawn() around the pcall for asynchronous execution allowing the coroutine to run without interrupting the main thread, this is not really the recommended fix though.

Im testing in studio, should i try in the actual game?

Can’t hurt to try in game!

If trying to set a datastore in studio, make sure you have Allow Studio Access to API turned on in the settings or datastore calls are not allowed.

yes, i also mentioned in the OP that i wasn’t getting any error/warnings
this is why im confused.

THIS WORKED!!!

thank you so much!!!
i really appreciate the help, have a nice day :slight_smile:

thank for your help, i try saving inside the actual game instead of studio and it worked

you probably havent allowed access to API services in game
you arent seeing anything because the pcall is probably failing if it works in game
try enabling that in game settings
useful tip if youre making datastores

i’ve alreayd fixed this issue thank you though

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