The goal is to save some of the settings the player can set in the game.
I have little experience in databases and I really can’t figure this thing out.
The script is this:
local DataStoreService = game:GetService("DataStoreService")
local LocalizationService = game:GetService("LocalizationService")
local RS = game:GetService("ReplicatedStorage")
local playerData = DataStoreService:GetDataStore("ExcursusData")
local Players = game:GetService("Players")
--local success, removedValue = pcall(function()
-- return playerData:RemoveAsync("Player_1175598905")
--end)
--if success then
-- print(removedValue)
--end
local function onPlayerJoin(player)
local storage = Instance.new("Folder")
storage.Name = "PlayerData"
storage.Parent = player
local VC = Instance.new("StringValue")
VC.Name = "SettingsVC"
VC.Parent = storage
local SettingsGQ = Instance.new("StringValue")
SettingsGQ.Name = "SettingsGQ"
SettingsGQ.Parent = storage
local playerUserId = "Player_" .. player.UserId
local data = playerData:GetAsync(playerUserId)
if data then
VC.Value = data
SettingsGQ.Value = data
else
VC.Value = "PART"
SettingsGQ.Value = "MED"
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, player.PlayerData.SettingsGQ.Value)
playerData:SetAsync(playerUserId, player.PlayerData.SettingsVC.Value)
end)
if not success then
warn('Error saving data!' .. tostring(err))
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
game:BindToClose(function() -- (only needed inside Studio)
if game:GetService("RunService"):IsStudio() then
wait(3)
end
end)
And it goes like this when I test:
I uncomment the function above to remove my data before entering.
Play the game, and no folder PlayerData shows up, which I think is ok.
Stop the game, comment the function, and play again
Folder shows up normal, I edit the values inside of it to see if they save (The values indeed are “MED” and “PART”, just like they should be):
Stop the game and play it again.
Both values are now set as “PART”, like how?
If anyone has an idea on what’s happening, I would really appreciate it.
And I am planning on adding more values, so yeah please be aware of that.
I’ve changed the script sections you corrected, but after testing for the first time it printed an error something like “… string expected, got nil”.
I thought it was something with changing the script so I manually changed the values, and stopped the game.
Every next playtest printed out no errors, but the values were always blank every time I tested.
That’s the script I now changed, have I missed something?
local DataStoreService = game:GetService("DataStoreService")
local LocalizationService = game:GetService("LocalizationService")
local RS = game:GetService("ReplicatedStorage")
local playerData = DataStoreService:GetDataStore("ExcursusData")
local Players = game:GetService("Players")
--local success, removedValue = pcall(function()
-- return playerData:RemoveAsync("Player_1175598905")
--end)
--if success then
-- print(removedValue)
--end
local function onPlayerJoin(player)
local storage = Instance.new("Folder")
storage.Name = "PlayerData"
storage.Parent = player
local VC = Instance.new("StringValue")
VC.Name = "SettingsVC"
VC.Parent = storage
local SettingsGQ = Instance.new("StringValue")
SettingsGQ.Name = "SettingsGQ"
SettingsGQ.Parent = storage
local playerUserId = "Player_" .. player.UserId
local success, err = pcall(function()
local data = playerData:GetAsync(playerUserId)
if data then
VC.Value = data.VC
SettingsGQ.Value = data.GQ
else
VC.Value = "PART"
SettingsGQ.Value = "MED"
end
end)
if not success then
warn('Error loading data! ' .. tostring(err))
end
end
local function onPlayerExit(player)
local success, err = pcall(function()
local playerUserId = "Player_" .. player.UserId
playerData:SetAsync(playerUserId, {
GQ = player.PlayerData.SettingsGQ.Value,
VC = player.PlayerData.SettingsVC.Value
})
end)
if not success then
warn('Error saving data! ' .. tostring(err))
end
end
game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)
game:BindToClose(function() -- (only needed inside Studio)
if game:GetService("RunService"):IsStudio() then
wait(3)
end
end)
Which values did you change them to in particular?
The error may have stemmed from the fact that the player’s data was still a singular string and the script was trying to find a function or property of it.
Also, make sure you’re editing/changing the values from the server and not the client