I am wondering if anyone else is having problems with an OrderedDataStore. Ran into hours of problems I was unable to fix today. This is a new problem with an old script. I even went back to the master script from a year ago I knew worked well. Again it wasn’t saving the data. Both the new and old programs when not in the studio are showing the same error in server logs with no line number or script name … “Unable to cast value to object.” Other DataStores are working fine. Was hoping someone with a OrderedDataStore could test this from themselves … This just happen today. I eliminated other possible causes and the error still persists. Has anything changed about the way these DataStores should be called? Save and Load wise … I am pretty sure the problem is with the command GetOrderedDataStore
I can confirm this is working perfectly in the studio. And I don’t see the error Unable to cast value to object. However when ran from a published program it will not save or read the data.
So no one is using a OrderedDataStore willing to take a second to see if it’s working or reply?
I did some testing and was able to get the same issue that you are describing (Unable to cast value to Object).
However, when I started messing around with adding a delay prior to the calling of it, it did show everything as working, and when I removed the delay it continued working. Can you please check if your issue still does occur? It’s possible it was fixed.
Code I used for testing
local DSS = game:GetService("DataStoreService"):GetOrderedDataStore("Test_2112Jay_1")
game.Players.PlayerAdded:Connect(function(plr)
print("GetAsync: ")
local success, err = pcall(function()
local loaded = DSS:GetAsync(plr.UserId)
warn("got", loaded)
end)
print("success: ", success)
print("error:", err)
end)
while true do
task.wait(10)
print("Set Async: ")
local success, err = pcall(function()
DSS:SetAsync(game.Players.PoppyandNeivaarecute.UserId, 10)
print("saved")
end)
print("success: ", success)
print("error:", err)
end
In your test … was it working in the studio and you never seen: Unable to cast value to Object?
But in the published game run did it not work and you seen: Unable to cast value to Object?
That error (warning) shows every time I run the published game right off the bat.
I’m working adding a pause. To see if this helps, unsuccessful so far.
This also is to update when the player is leaving the game.
This part seem to be having problems it never had before even in the studio.
local dataStoreService = game:GetService("DataStoreService")
local dataKey = dataStoreService:GetOrderedDataStore("Coins")
local plrID, coins = nil, nil
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder")
ls.Name = "leaderstats" ls.Parent = plr
local cn = Instance.new("IntValue")
cn.Name = "Coins" cn.Parent = ls
coins = plr.leaderstats.Coins
-- plrID = tonumber(plr.UserId) -- not sure about this tried it both ways
plrID = plr.UserId
local sus, err = pcall(function()
ls:WaitForChild("Coins").Value = dataKey:GetAsync(plrID, "Coins") or 0
end) if not sus then warn(err) end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local sus, err = pcall(function()
dataKey:SetAsync(plrID, coins.Value)
end) if not sus then warn(err) end
end)
WELL … we just had an update and now it isn’t working in the Studio also.
Was working on this, closed out and reloaded back to it not working in Studio.
However it is now outputting “Unable to cast value to Object” in studio also.
Now nothing works …
I just changed the GetAsycn(playerid,“Coins”) to just playerid, that returns the number.
Here is my script version, is actually the same but writen with my logic:
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetOrderedDataStore("Coins")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local coinsValue = Instance.new("IntValue")
coinsValue.Name = "Coins"
coinsValue.Parent = leaderstats
local plrID = plr.UserId
local success, storedCoins = pcall(function()
return dataStore:GetAsync(tostring(plrID))
end)
if success then
coinsValue.Value = storedCoins or 0
else
warn("Failed to retrieve coins from DataStore for player:", plr.Name)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local plrID = plr.UserId
local coins = plr.leaderstats and plr.leaderstats.Coins
if coins then
local success, errorMessage = pcall(function()
dataStore:SetAsync(tostring(plrID), coins.Value)
end)
if not success then
warn("Failed to save coins to DataStore for player:", plr.Name, errorMessage)
end
end
end)
Got it working with your help. I can’t thank you enough. Saved years of work. Thank you!
local dataStoreService = game:GetService("DataStoreService")
local dataKey = dataStoreService:GetOrderedDataStore("Coins")
game.Players.PlayerAdded:Connect(function(plr)
local ls = Instance.new("Folder") ;ls.Name = "leaderstats" ;ls.Parent = plr
local cn = Instance.new("IntValue") ;cn.Name = "Coins" ;cn.Parent = ls
local sus, err = pcall(function()
plr.leaderstats.Coins.Value = dataKey:GetAsync(plr.CharacterAppearanceId) or 0
end) if not sus then warn(err) end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local sus, err = pcall(function()
dataKey:SetAsync(plr.CharacterAppearanceId, plr.leaderstats.Coins.Value)
end) if not sus then warn(err) end
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.