I am using a datastore to save an ObjectValue. However, when I try to it says:
15:13:04.266 - ServerScriptService.Scripts.DataStore:20: bad argument #3 to 'Value' (Object expected, got number)
What am I doing wrong?
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("StatsDataStore") game.Players.PlayerAdded:Connect(function(player) local PlayerStats = Instance.new("Folder", player) PlayerStats.Name = "PlayerStats" local Points = Instance.new("IntValue", PlayerStats) Points.Name = "Points" Points.Value = ds1:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId, Points.Value) local SpawnPoint = Instance.new("ObjectValue", PlayerStats) SpawnPoint.Name = "SpawnPoint" SpawnPoint.Value = ds1:GetAsync(player.UserId) or game.Workspace.spawn.Spawn1 --error occurs here ds1:SetAsync(player.UserId, SpawnPoint.Value) Points.Changed:Connect(function() ds1:SetAsync(player.UserId, Points.Value) print("saved points") end) SpawnPoint.Changed:Connect(function() ds1:SetAsync(player.UserId, SpawnPoint.Value) end) end)
You're saving numbers in your script to ds1. You're trying to set ObjectValue to a number (your points value) on line 20 because the loaded data IS a number. ObjectValue's Value is supposed to be an Object, not a number. You're also saving over one data store with different data (SpawnPoint's Value and Points Value). You need to use separate stores or use a table.
local DataStore = game:GetService("DataStoreService") local main_store = DataStore:GetDataStore("StatsDataStore") local function save_stats(key, Points, Spawnpoint) local saved = { ['Points'] = Points; ['SpawnPoint'] = Spawnpoint; } if pcall(function() main_store:SetAsync(key, saved) end) then print("data saved") else print("data did not save") end end -- This is the folder that contains all your spawns! local spawns = workspace:WaitForChild('Spawns') game.Players.PlayerAdded:Connect(function(player) local key = string.format("%s_%s", player.Name, player.UserId) local saved_key = main_store:GetAsync(key) or false local saved_points = saved_key and saved_key.Points or 0 -- Put a default spawn in your folder, this will be the spawn that gets set if nothing is saved -- replace 'default_spawn' with whatever you name yours local saved_spawn = saved_key and saved_key.SpawnPoint or 'default_spawn' print(saved_key, saved_points, saved_spawn) local PlayerStats = Instance.new("Folder") PlayerStats.Name = "PlayerStats" local Points = Instance.new("IntValue", PlayerStats) Points.Name = "Points" Points.Value = saved_points local SpawnPoint = Instance.new("ObjectValue", PlayerStats) SpawnPoint.Name = "SpawnPoint" -- Remember saved spawn is a name, so we have to look for it SpawnPoint.Value = spawns[saved_spawn] PlayerStats.Parent = player -- You should be using this in something like -- StarterCharacterScripts with RemoteEvents Points.Changed:Connect(function() -- Can't save objects, save the value or name. save_stats(key, Points.Value, SpawnPoint.Value.Name) end) SpawnPoint.Changed:Connect(function() -- Can't save objects, save the value or name. save_stats(key, Points.Value, SpawnPoint.Value.Name) end) end)