Data Saving Trouble With

I want to data save some values, multiple values but I do not know how.
Not sure how I cannot figure out how to data save and get the data

Do not know any solutions

local storda = game:GetService("DataStoreService"):GetDataStore("ArrestsandCitationsCriminalData")

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(character)
		if Player:FindFirstChild("Target") == nil then
			local StringWowValYes = Instance.new("StringValue")
			StringWowValYes.Parent = Player
			StringWowValYes.Name = "Target"
		end
		if Player:FindFirstChild("Cuffed") == nil then
			local BoolValYesOk = Instance.new("BoolValue")
			BoolValYesOk.Parent = Player
			BoolValYesOk.Name = "Cuffed"
		end
		if Player:FindFirstChild("Grabbed") == nil then
			local BoolValYesOk = Instance.new("BoolValue")
			BoolValYesOk.Parent = Player
			BoolValYesOk.Name = "Grabbed"
		end
		if Player:FindFirstChild("Arrests") == nil then
			local ArrestValu = Instance.new("NumberValue")
			ArrestValu.Parent = Player
			ArrestValu.Name = "Arrests"
			ArrestValu.Value = storda:GetAsync(Player.UserId)
		end
		if Player:FindFirstChild("Citations") == nil then
			local CitatValu = Instance.new("NumberValue")
			CitatValu.Parent = Player
			CitatValu.Name = "Citations"
			CitatValu.Value = storda:GetAsync(Player.UserId)
		end
		if Player:FindFirstChild("Warrants") == nil then
			local WarrVa = Instance.new("BoolValue")
			WarrVa.Parent = Player
			WarrVa.Name = "Warrants"
			WarrVa.Value = storda:GetAsync(Player.UserId)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(player)
	storda:SetAsync(player.UserId, player.Arrests.Value, player.Citations.Value, player.Warrants.Value)
end)

Find first child does not return nil as i know from my knowledge.

If the instance does not exist, :findfirstchild will return bool false, if it does exist then it return bool true.

And also, i see that you didn’t put :SetAsync at the PlayerAdded datastore. Without setting a data yet, the value would just return nil

Unfortunately both of those solutions are not the problem, thank you for the assistance though!!

1 Like

Try this template?

game:GetService("Players").PlayerAdded:Connect(function()

-- Creating your instances here

-- data store script
local Datastore__key = -- key format here
local GetData = YourDatastore:GetAsync(Datastore__key)

if GetData then -- the user already have a data
Value1.Value = GetData[1]
else
local SetData = {Value1.Value}
YourDatastore:SetAsync(Datastore__key, SetData)
end
end)

what is a key format my sir? can you tell me

Key is where you log in to the data, for instance like what you did, Player.UserId

I have it down but now its a problem because it says “Unable to Cast Array” so thats the problem

local storda = game:GetService("DataStoreService"):GetDataStore("ArrestsandCitationsCriminalData")

game:GetService("Players").PlayerAdded:Connect(function(Player)
	-- Creating your instances here
	if Player:FindFirstChild("Target") == nil then
		local StringWowValYes = Instance.new("StringValue")
		StringWowValYes.Parent = Player
		StringWowValYes.Name = "Target"
	end
	if Player:FindFirstChild("Cuffed") == nil then
		local BoolValYesOk = Instance.new("BoolValue")
		BoolValYesOk.Parent = Player
		BoolValYesOk.Name = "Cuffed"
	end
	if Player:FindFirstChild("Grabbed") == nil then
		local BoolValYesOk = Instance.new("BoolValue")
		BoolValYesOk.Parent = Player
		BoolValYesOk.Name = "Grabbed"
	end
	if Player:FindFirstChild("Arrests") == nil then
		local ArrestValu = Instance.new("NumberValue")
		ArrestValu.Parent = Player
		ArrestValu.Name = "Arrests"
	end
	if Player:FindFirstChild("Citations") == nil then
		local CitatValu = Instance.new("NumberValue")
		CitatValu.Parent = Player
		CitatValu.Name = "Citations"
	end
	if Player:FindFirstChild("Warrants") == nil then
		local WarrVa = Instance.new("BoolValue")
		WarrVa.Parent = Player
		WarrVa.Name = "Warrants"
	end
	
	-- data store script
	local Datastore__key = Player.UserId
		local GetData = storda:GetAsync(Datastore__key)

	if GetData then -- the user already have a data
		Player.Arrests.Value = GetData[1]
		Player.Citations.Value = GetData[2]
		Player.Warrants.Value = GetData[3]
	else
		local SetData = {GetData[1], GetData[2], GetData[3]}
		storda:SetAsync(Datastore__key, SetData)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	storda:SetAsync(player.UserId, player.Arrests.Value, player.Citations.Value)
end)

Try this one

local datastoreservice = game:GetService("DataStoreService")
local saveenergy = datastoreservice:GetDataStore("ArrestsandCitationsCriminalData")
local playeronserver = 0
local bindableEvent = Instance.new("BindableEvent")
game.Players.PlayerAdded:Connect(function(player)
	playeronserver  += 1
	local playerid = "Player_1234567"..player.UserId
	local folder= Instance.new("Folder",player)
	folder.Name = "stuff"


	local StringWowValYes = Instance.new("StringValue")
	StringWowValYes.Parent = folder
	StringWowValYes.Name = "Target"
	
	local BoolValYesOk = Instance.new("BoolValue")
	BoolValYesOk.Parent = folder
	BoolValYesOk.Name = "Cuffed"
	
	local plsmakeitdiffrentname = Instance.new("BoolValue")
	plsmakeitdiffrentname.Parent = folder
	plsmakeitdiffrentname.Name = "Grabbed"
	
	local ArrestValu = Instance.new("NumberValue")
	ArrestValu.Parent = folder
	ArrestValu.Name = "Arrests"
	
	
	local CitatValu = Instance.new("NumberValue")
	CitatValu.Parent = folder
	CitatValu.Name = "Citations"
	
	
	local WarrVa = Instance.new("BoolValue")
	WarrVa.Parent = folder
	WarrVa.Name = "Warrants"


	local data
	local success, errormessage = pcall(function()
		data = saveenergy:GetAsync(playerid) --- getting player datastore
	end)

	if success then
		print(data)
		if data ~= nil then --- its will load old player data
			for i,v in pairs(folder:GetChildren()) do
				v.Value = data[v.Name]
			end
		elseif data == nil then --- if when player 1st time join
			StringWowValYes.Value = "Nothing"
			BoolValYesOk.Value = false
			plsmakeitdiffrentname.Value = false
			ArrestValu.Value = 0
			CitatValu.Value = 0
			WarrVa.Value = false
		end
	else
		warn(errormessage)
	end

end)


game.Players.PlayerRemoving:Connect(function(player) ---- saving when player leaving server
	local playerid = "Player_1234567"..player.UserId	
	local tab = {}
	
	for i,v in pairs(player:FindFirstChild("stuff"):GetChildren()) do
		tab[v.Name] = v.Value 
	end
	
	local success, errormessage = pcall(function()
		saveenergy:SetAsync(playerid, tab) ---- saving stuff
	end)



	if success then
		print(tab,"succses to save")
		playeronserver -= 1
		bindableEvent:Fire()
	else
		print("Data failed to save")
		warn(errormessage)
	end
end)


game:BindToClose(function() --- waiting player leave before closing the game
	while playeronserver > 0 do
		bindableEvent.Event:Wait()
	end
end)
1 Like

You placed GetData[1] and so on. You must put Arrests.Value. and don’t use if Player:FindFirstChild("Warrants") == nil then since there’s no instance created when they first joined until you make it.

This worked thank you. I really appreciate this man thank you.

1 Like