String value not saving in dataStore

I have a simulator-type game using a linear layout that is made up of 5 zones. (like the pet sim x zones) I have a folder inside the player that is made up of 5 values indicating if the player owns the zone or not. I am trying to save those values into the roblox dataStores. The issue is that my data will not load properly.

local Players = game:GetService('Players')
local DSS = game:GetService("DataStoreService")
local zonesOwned = DSS:GetDataStore("zonesOwned")
-- Getting a data store


game.Players.PlayerAdded:Connect(function(plr)
	
	local ZoneOwnershipValueFolder = plr:WaitForChild("ZoneOwnershipValueFolder")
	local Zone2 = ZoneOwnershipValueFolder.Zone2Value
	local Zone3 = ZoneOwnershipValueFolder.Zone3Value
	local Zone4 = ZoneOwnershipValueFolder.Zone4Value
	local Zone5 = ZoneOwnershipValueFolder.Zone5Value
	local Zone6 = ZoneOwnershipValueFolder.Zone6Value
-- Getting the zones
	
	
	local data
	local success, errormessage = pcall(function()
		 data = zonesOwned:GetAsync(plr.UserId)-- getting a place to store the data
	end)
	
	
	if data ~= nil then 
		Zone2.Value = data[1]
		Zone3.Value = data[2]
		Zone4.Value = data[3]
		Zone5.Value = data[4]
		Zone6.Value = data[5]
		print("Successfully gotten data")
		-- tying the ZoneValues to data
	else
		print("unable of getting player data")
		warn(errormessage)
		
	end
	
end)




local function OnPlayerRemoving(plr)
	
	local save = {}
	table.insert(save,plr.ZoneOwnershipValueFolder.Zone2Value.Value)
	table.insert(save,plr.ZoneOwnershipValueFolder.Zone3Value.Value)
	table.insert(save,plr.ZoneOwnershipValueFolder.Zone4Value.Value)
	table.insert(save,plr.ZoneOwnershipValueFolder.Zone5Value.Value)
	table.insert(save,plr.ZoneOwnershipValueFolder.Zone6Value.Value)
	-- The values again
	

	print(save)
	local success, errormessage = pcall(function()
		zonesOwned:SetAsync(plr.UserId,save)-- Setting a place to store the data
		end)
	if success then 
		print("Data Has been Saved")
	else
		warn(errormessage)
	end
end

	Players.PlayerRemoving:Connect(OnPlayerRemoving)
	
	game:BindToClose(function()
		for i, player in pairs(Players:GetChildren()) do
		OnPlayerRemoving(player)
		-- checking when the plr left
		end
	end)


You can see the value changed but it did not save

Screenshot 2022-08-09 102143
Also tell me if you need the script that controls the GUI that says 'Would you like to buy zone3 for 50l"

Player.PlayerRemoving doesn’t work in Studio meaning that of your data ever saves.

first make sure that the studio to API access is open

second make sure that the value is server side changed not from client side(local script) if you want to check if it changed from the server or the client then switch to server and check the value of the string

This is a ServerScript and the reason it most likely isn’t working is because PlayerRemoving event doesn’t fire in Studio.

Screenshot 2022-08-09 170317
Ok i tried it in the game not studio and this is what it printed

yea, but according to his output the PlayerRemoving event is fired

Well does the data save now or not?

PlayerRemoving doesn’t work in Studio.

Oh wait I changed the value using a local script.

local yes = script.Parent
local plr = game.Players.LocalPlayer

yes.MouseButton1Click:Connect(function()
	if plr.leaderstats.Coins.Value >= 10000 then
		plr.leaderstats.Coins.Value = plr.leaderstats.Coins.Value - 10000
		game.Players.LocalPlayer.PlayerGui.Zone2Purchase.Enabled = false 
		game.Players.LocalPlayer.PlayerGui.Zone2Purchase.PurchasePromptScript2.Disabled = true
		
		local Zone2Value = game.Players.LocalPlayer.ZoneOwnershipValueFolder.Zone2Value
		Zone2Value.Value = "Owned"
		local Exit1 = game.workspace.Zone1.Exit1:Destroy()
		
	else 
		print("Player Can't Afford to to buy Zone1")
	
	end
end)

How do I delete the wall tho in the server script cause I delete the barrier from the server everybody will be able to go in the area, I think i should use remote function

use a RemoteEvent or a RemotFunction

1 Like