Script gets stuck on loading data

I have this simple data loading script, but for some reason the script halts at the moment of setting value to an integer. It won’t throw any error or anything, it just stops. I know this because I get nothing in the output, and the code after plrCredits.Value = Data["Credits"] or 0 wont execute - > value “Tokens” is never created.
I should also mention that I have studio API acces turned on, and when I comment out plrCredits.Value = Data["Credits"] or 0 then value “Tokens” is created but script gets stuck again on plrTokens.Value = Data["Tokens"] or 50

my script:

local players = game:GetService("Players")
local replicated = game:GetService("ReplicatedStorage")
local DataStoreService = game:GetService("DataStoreService")
local playerData = DataStoreService:GetDataStore("PlayerData")

players.PlayerAdded:Connect(function(player)
	local succes, errorMessage = pcall(function()
		local playerID = player.UserId
		local key = "P_" .. playerID
		local Data = nil
		local plrFolder = Instance.new("Folder")
		plrFolder.Name = player.Name
		plrFolder.Parent = replicated.UserData
		local succes, error = pcall(function()
			local Data = playerData:GetAsync(key)
		end)
		
		local plrCredits = Instance.new("IntValue")
		plrCredits.Name = "Credits"
		plrCredits.Parent = plrFolder
		plrCredits.Value = Data["Credits"] or 0
		
		local plrTokens = Instance.new("IntValue")
		plrTokens.Name = "Tokens"
		plrTokens.Parent = plrFolder
		plrTokens.Value = Data["Tokens"] or 50
	end)
end)

1 Like

Change that code to this:

local ran, result = pcall(function()
	return playerData:GetAsync(key)
end)
if ran then Data = result end

No change, but I noticed that I have forgot to print error of the first pcall, and I not get error that says: Attampt to index nil with “Credits”.

Ah. So after if ran then Data = result end, add this line:

Data = Data or {}

Can you provide the script which saves the data?

That would be useless because first time players won’t trigger it anyways

Do you count as a first time player?

1 Like

What do you mean? If the player has no data, their data is nil. So defaulting it to an empty dictionary will prevent the error you got.

Still no difference, same error

Try adding

error(errrorMessage)

Pcall functions should always stop and continue from where it stopped in the next section of it- that’s what it’s for.

Can you show your updated script as it is now?

image

here you go

I think also you should remove this top pcall because it’s preventing you from seeing errors.

I started printing them on the end, so its ok

The issue is when you say local Data = ... in the second pcall, the variable is lost when the pcall ends, because it is locally inside the pcall, so no other code can access it. Instead, use this:

I got it like this
image
but still the same thing

local succes, errorMessage = pcall(function()
		local playerID = player.UserId
		local key = "P_" .. playerID
		local Data
		local plrFolder = Instance.new("Folder")
		plrFolder.Name = player.Name
		plrFolder.Parent = replicated.UserData
		local succes, error = pcall(function()
			Data = playerData:GetAsync(key)
		end)

like this

Don’t forget to also add this line after if succes then Data = result end:

Data = Data or {}

both solutions work! thank you guys for help

1 Like

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