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)
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
Hello I Just tested you’re original code and it’s working good:
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
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)