Data Store doesn't save string values for some reason

I have made a ban script, the bool value is getting saved successfully. But the string Value isn’t getting saved.

Here’s the code:

local Ban_Value = 3

local Players = game.Players
local dataStore = game:GetService("DataStoreService")
local MyData = dataStore:GetDataStore("BanData_"..Ban_Value)

Players.PlayerAdded:Connect(function(player)
	local Key = "Player_"..player.UserId
	
	local Ban_Folder = Instance.new("Folder", player)
	Ban_Folder.Name = "Ban_Folder"
	
	local Ban_Status = Instance.new("BoolValue", Ban_Folder)
	Ban_Status.Name = "Is_Banned"
	
	local Reason_Text = Instance.new("StringValue", Ban_Folder)
	Reason_Text.Name = "Reason"
	
	local data = MyData:GetAsync(Key)
	
	Ban_Status.Value = data or false
	Reason_Text.Value = data or ""
end)

Players.PlayerRemoving:Connect(function(player)
	local Key = 'Player_'..player.UserId
	
	local Ban_Folder = player:WaitForChild("Ban_Folder")
	
	if Ban_Folder then
		local Is_Banned = Ban_Folder.Is_Banned.Value
		local Reason = Ban_Folder.Reason.Value
		MyData:SetAsync(Key, Is_Banned)
		MyData:SetAsync(Key, Reason)
	end
end)
  1. why are you loading bans to a folder if player will get kicked as he joins?
  2. you dont need to add Player_ to the key, just the id
  3. youre saving 2 values to the same datastore key
  4. you should use pcalls as saving and loading can fail
2 Likes
  1. Just for better organization, it doesn’t seem to affect it very much.
  2. On this one I don’t know why I use Player_ to the key.
  3. Do I have to use Tables or 2 different data stores?
  4. Haven’t learned to do that yet but thanks for the tip.

you can do both

local data = {
	["is_banned"] = Is_Banned,
	["reason"] = Reason
}
MyData:SetAsync(Key, data)

-- when getting

data = MyData:GetAsync(Key)
local isbanned = data[is_banned]
local reason = data[reason]

also make sure to use pcalls and maybe retries if needed

2 Likes

The code that you have provided doesn’t seem to really work.

1 Like

Hello I Just tested you’re original code and it’s working good:
image
image

Instead test this on studio test it in a Local Server or in the normal game (I Noticied that Roblox studio doest not always data when leaving the game) on play test

1 Like

Got it to work.

Here’s the code:

function loadData(player)
	local Key = "Player_"..player.UserId
	
	local Ban_Folder = Instance.new("Folder", player)
	Ban_Folder.Name = "Ban_Folder"
	
	local Ban_Status = Instance.new("BoolValue", Ban_Folder)
	Ban_Status.Name = "Ban_Status"
	
	local Ban_Message = Instance.new("StringValue", Ban_Folder)
	Ban_Message.Name = "Ban_Message"
	
	local succes, err = pcall(function()
		local data = DataStore:GetAsync(Key)
		
		if data then
			Ban_Status.Value = data[1]
			Ban_Message.Value = data[2]
		else
			Ban_Status.Value = false
			Ban_Message.Value = ""
		end
	end)
	
	if err then
		print("Could not load data!")
		warn(err)
	end
end

function saveData(player)
	local Key = "Player_"..player.UserId
	local Ban_Folder = player:WaitForChild("Ban_Folder")
	
	local succes, err = pcall(function()
		
		local stufftoSave = {
			Ban_Folder.Ban_Status.Value,
			Ban_Folder.Ban_Message.Value,
		}
		
		if Ban_Folder then
			DataStore:SetAsync(Key, stufftoSave)
		end
		
	end)
	
	if err then
		print("Could not save data!")
		warn(err)
	end
end

function onServerShutdown()
	if game:GetService("RunService"):IsStudio() then
		wait(2)
	else
		for _, player in pairs(Players:GetPlayers()) do
			local finished = Instance.new("BindableEvent")
			local Ban_Folder = player:WaitForChild("Ban_Folder")
			local Key = "Player_"..player.UserId
			
			local stufftoSave = {
				Ban_Folder.Ban_Status.Value,
				Ban_Folder.Ban_Message.Value,
			}
			
			if Ban_Folder then
				DataStore:SetAsync(Key, stufftoSave)
			end
		finished.Event:Wait()
		end		
	end
end

game:BindToClose(onServerShutdown)
Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)
2 Likes