hello, im trying to make a datastore for my game, but the datastore doesn't work and it shows " Unable to cast value to Object" error in output.
Code (ServerScript)
local DataStoreService = game:GetService("DataStoreService")
local key = "ComputerSimulatorTestingPhase2.0.1"
local myDataStore = DataStoreService:GetDataStore(key)
game.Players.PlayerAdded:connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue") --Change points to whatever you want, unless you want to keep it
coins.Name = "Coins" --Name of the Points, or whatever name you changed it to
coins.Parent = leaderstats
local gems = Instance.new("IntValue") --Change wins to whatever you want, unless you want to keep it
gems.Name = "Gems" --Name of the Wins, or whatever name you changed it to
gems.Parent = leaderstats
local pcs = Instance.new("IntValue")
pcs.Name = "PCs"
pcs.Parent = leaderstats
local maxpcs = Instance.new("NumberValue")
maxpcs.Name = "MaxPCs"
maxpcs.Parent = leaderstats
maxpcs.Value = 10
local MaxPCsUpgrade = Instance.new("Folder")
MaxPCsUpgrade.Name = "MaxPCsUpgrade"
MaxPCsUpgrade.Parent = player
local newmaxpcs = Instance.new("NumberValue")
newmaxpcs.Name = "NewMaxPCs"
newmaxpcs.Value = 10
newmaxpcs.Parent = MaxPCsUpgrade
local boxes = Instance.new("NumberValue")
boxes.Name = "Boxes"
boxes.Value = 0
boxes.Parent = leaderstats
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-coins", player.UserId.."-gems", player.UserId.."-pcs", player.UserId.."-maxpcs", player.UserId.."-boxes")
end)
if success then
coins.Value = data
wait(0.5)
gems.Value = data
wait(0.5)
pcs.Value = data
wait(0.5)
maxpcs.Value = data
wait(0.5)
boxes.Value = data
print("data loaded")
else
warn(errormessage)
player:Kick(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-coins", player.leaderstats.Coins.Value, player.UserId.."-gems", player.leaderstats.Gems.Value, player.UserId.."-pcs", player.leaderstats.PCs.Value, player.UserId.."maxpcs", player.leaderstats.MaxPCs.Value, player.UserId.."-boxes", player.leaderstats.Boxes.Value)
end)
if success then
print("Data saved")
else
print("An error occured while saving data")
warn(errormessage)
player:Kick(errormessage)
end
end)

2 comments
at what line?
it doesn't say since it's warn(errormessage)