Pcall isn't doing anything

I’m trying to save a table using :JSONEncode() and :JSONDecode(). The loading of data works just fine, but the saving doesn’t work, the pcall doesn’t print anything, even errors.

Here’s the saving code

game.Players.PlayerRemoving:connect(function(plr)
	local inv = {}

	for i, v in pairs(plr.Values.Plot.Value.Dirt:GetChildren()) do
		inv[v.Name] = v.Dirt.Value
		inv[v.Name.."WeightMin"] = v.Dirt.WeightRange.Min.Value
		inv[v.Name.."WeightMax"] = v.Dirt.WeightRange.Max.Value
	end 
	local success, errormessage = pcall(function()
		ds:SetAsync(plr.UserId.."dirt", https:JSONEncode(inv))
        -- nothing in here prints
	end)
	if success then
		print("Saved") -- not printing
	else
		warn(errormessage) -- not printing
	end
	
end)

Picture of the dirt object tree
image

Here’s loading code

local newData
local success, errorMessage = pcall(function()
	 newData = ds:GetAsync(plr.UserId.."dirt")
end)
if success then
	print("Loaded")
else
	warn(errorMessage)
end
if newData then
	local dt = https:JSONDecode(newData)
	for i, v in pairs(dt) do
		local dir = cl.Parent.Parent.Dirt:FindFirstChild(i)
		if dir then
			dir.Dirt.Value = v
			for o, c in pairs(dt) do
				if o == dir.Name.."WeightMin" then
					dir.Dirt.WeightRange.Min.Value = c
				elseif o == dir.Name.."WeightMax" then
					dir.Dirt.WeightRange.Max.Value = c
				end
			end
		end
	end 
end

Does this “ds:SetAsync(plr.UserId…“dirt”, https:JSONEncode(inv))” code part even run? or the entire pcall function is just existing but not working or just the results are the problem?

1 Like

the pcall isn’t working. if the JSONEncode was working, the pcall would print something (i think)

1 Like

Try to print out something after the encode to make sure the pcall is running them inside or not.

local success, errormessage = pcall(function()
	ds:SetAsync(plr.UserId.."dirt", https:JSONEncode(inv))
	print("hello") -- no print
end)

Are you sure PlayerRemoving is firing?

yes

game.Players.PlayerRemoving:Connect(function(plr)
	local inv = {}

	for i, v in pairs(plr.Values.Plot.Value.Dirt:GetChildren()) do
		inv[v.Name] = v.Dirt.Value
		inv[v.Name.."WeightMin"] = v.Dirt.WeightRange.Min.Value
		inv[v.Name.."WeightMax"] = v.Dirt.WeightRange.Max.Value
	end 
	print("hello") -- hello
	local success, errormessage = pcall(function()
		ds:SetAsync(plr.UserId.."dirt", https:JSONEncode(inv))
		print("hello") -- nothing
	end)
	if success then
		print("Saved")
	else
		warn(errormessage)
	end
	
end)

Hmm

I’ve never used http: so

I can only guess that the code in the pcall never finishes running and is stuck doing, something

Actually

Try running a 2-player test in studio and make one player leave. If it runs, the issue is the test stops before the code finishes.

Have you tried doing this yet?

No problems in the Code though… (Pretty sure it’s a studio bug)

It worked, but how do i make it so that the code finishes before the test?

You can use game:BindToClose() in any script to execute some code before the game closes, which could happen when the final player leaves. You can add any code and the game will delay the closure of the server with 30 seconds max to execute your code. Of course, you don’t need the full 30 seconds so maybe just try:

game:BindToClose(function()
	wait(5)
end)
1 Like

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