Pcall function isnt running

Alright, ill try to explain whats happening as clearly as possible.
So I have a couple of parts in my workspace, which have a few attributes. My aim is to save the values of these attributes.
Now, ive got the script done (at least i think so)
Although, in the player removing function the pcall function doesnt seem to run, and in the :BindToClose function, the script doesnt seem to continue after a loop

My code:

local DataStoreService = game:GetService("DataStoreService")
local gridDataStore = DataStoreService:GetDataStore("GridDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local GridFolder = game.Workspace.Grid
	local key = player.UserId
	local data
	local success,errormessage = pcall(function()
		print('adding pcall')
		data = gridDataStore:GetAsync(key)
	end)
	if success then
		print("success loading the data")
		if data~= nil then
			print("data is not nil")
			print(data)
			local index = 0
			for i,v in pairs(data) do
				index += 1
				GridFolder[tostring(index)]:SetAttribute("BuildingName", data[tostring(index)]["BuildingName"]) 
				GridFolder[tostring(index)]:SetAttribute("BuildingStatus", data[tostring(index)]["BuildingStatus"]) 
				GridFolder[tostring(index)]:SetAttribute("Floors", data[tostring(index)]["Floors"]) 
				GridFolder[tostring(index)]:SetAttribute("Income", data[tostring(index)]["Income"]) 
			end
		end
	else
		print(errormessage)
	end


	-- getasync
end)


game.Players.PlayerRemoving:Connect(function(player)
	--setasync
	print("player removing")
	local data = {}
	local GridFolder = game.Workspace.Grid
	for i,v in pairs(GridFolder:GetChildren()) do
		if v.ClassName == 'Script' then return end
		local index = tonumber(v.Name)
		local t = {
			["BuildingName"] = v:GetAttribute("BuildingName"),
			["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
			["Floors"] = v:GetAttribute("Floors"),
			["Income"] = v:GetAttribute("Income")
		}
		data[tostring(index)] = t
		
	end
	local success,errormessage = pcall(function()
		print('removing pcall')
		gridDataStore:SetAsync(player.UserId,data)
	end)
	if success then 
		print("Data saved") 
	else
		warn("DATA NOT SAVED"..errormessage) 
	end
end)

game:BindToClose(function()
	for k,player in pairs(game.Players:GetPlayers()) do
		print('game closing')
		local data = {}
		local GridFolder = game.Workspace.Grid
		for i,v in pairs(GridFolder:GetChildren()) do
			if v.ClassName == 'Script' then return end
			local index = tonumber(v.Name)
			local t = {
				["BuildingName"] = v:GetAttribute("BuildingName"),
				["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
				["Floors"] = v:GetAttribute("Floors"),
				["Income"] = v:GetAttribute("Income")
			}
			
			data[tostring(index)] = t
		end
	
			print('removing pcall')
			gridDataStore:SetAsync(player.UserId,data)
		
	end
end)

the “removing pcall” print statement never prints in the player removing function or bind to close. And the data isnt being saved either.

Also ive tried testing it in game in roblox, in studio as well. I’ve turned on all the necessary toggles in settings-security as well

All help is appreciated.

Is your script serversided? if its in the client side then i recommend changing it now. Also, is your script inside of serverscriptservice?

Yeah its a serverscript and its in serverscriptservice

It looks like the issue is with your return statement in the PlayerRemoving and BindToClose functions. The return statement will immediately exit the for loop and function, so only the first child of GridFolder is being processed in both functions.

Here’s the updated code with the return statement removed and some other minor fixes:

local DataStoreService = game:GetService("DataStoreService")
local gridDataStore = DataStoreService:GetDataStore("GridDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local GridFolder = game.Workspace.Grid
    local key = player.UserId
    local data
    local success,errormessage = pcall(function()
        print('adding pcall')
        data = gridDataStore:GetAsync(key)
    end)
    if success then
        print("success loading the data")
        if data~= nil then
            print("data is not nil")
            print(data)
            local index = 0
            for i,v in pairs(data) do
                index += 1
                GridFolder[tostring(index)]:SetAttribute("BuildingName", data[tostring(index)]["BuildingName"]) 
                GridFolder[tostring(index)]:SetAttribute("BuildingStatus", data[tostring(index)]["BuildingStatus"]) 
                GridFolder[tostring(index)]:SetAttribute("Floors", data[tostring(index)]["Floors"]) 
                GridFolder[tostring(index)]:SetAttribute("Income", data[tostring(index)]["Income"]) 
            end
        end
    else
        print(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    print("player removing")
    local data = {}
    local GridFolder = game.Workspace.Grid
    for i,v in pairs(GridFolder:GetChildren()) do
        if v.ClassName ~= 'Script' then -- fix the return statement to if statement
            local index = tonumber(v.Name)
            local t = {
                ["BuildingName"] = v:GetAttribute("BuildingName"),
                ["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
                ["Floors"] = v:GetAttribute("Floors"),
                ["Income"] = v:GetAttribute("Income")
            }
            data[tostring(index)] = t
        end
    end
    local success,errormessage = pcall(function()
        print('removing pcall')
        gridDataStore:SetAsync(player.UserId,data)
    end)
    if success then 
        print("Data saved") 
    else
        warn("DATA NOT SAVED"..errormessage) 
    end
end)

game:BindToClose(function()
    for k,player in pairs(game.Players:GetPlayers()) do
        print('game closing')
        local data = {}
        local GridFolder = game.Workspace.Grid
        for i,v in pairs(GridFolder:GetChildren()) do
            if v.ClassName ~= 'Script' then -- fix the return statement to if statement
                local index = tonumber(v.Name)
                local t = {
                    ["BuildingName"] = v:GetAttribute("BuildingName"),
                    ["BuildingStatus"] = v:GetAttribute("BuildingStatus"),
                    ["Floors"] = v:GetAttribute("Floors"),
                    ["Income"] = v:GetAttribute("Income")
                }
                data[tostring(index)] = t
            end
        end
        print('removing pcall')
        gridDataStore:SetAsync(player.UserId,data)
    end
end)

Yep it works now, I didnt notice that the entire function was being stopped. Thank you

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