What do you want to achieve? Keep it simple and clear!
I want to save the players money, xp, and level.
What is the issue? Include screenshots / videos if possible!
It won’t save. It always comes up with this error;
13:10:21.510 Unable to cast value to Object - Server - leaderstats:49
My code;
local DSS = game:GetService("DataStoreService")
local MDS = DSS:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local EP = Instance.new("IntValue")
EP.Name = "Evolution Points"
EP.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local data
local data2
local data3
local success, errormessage = pcall(function()
data = MDS:GetAsync(plr.UserId.."-Evolution Points")
data2 = MDS:GetAsync(plr.UserId.."-XP")
data3 = MDS:GetAsync(plr.UserId.."-Level")
end)
if success then
EP.Value = data
XP.Value = data2
Level.Value = data3
else
print("Failed!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
MDS:SetAsync("id_"..plr.UserId, plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value)
end)
if success then
print("Success!")
else
print("Failed!")
warn(errormessage)
end
end)
I tried to make a different script to fix this problem, here is it’s code;
local DataStoreService = game:GetService("DataStoreService")
local StoreData = DataStoreService:GetDataStore("StoreleaderstatsData")
game.Players.PlayerAdded:Connect(function(plr)
wait()
local plrkey = "id_"..plr.UserId
local Save1 = plr.leaderstats:FindFirstChild("Evolution Points")
local Save2 = plr.leaderstats:FindFirstChild("XP")
local Save3 = plr.leaderstats:FindFirstChild("Level")
local GetSaved = StoreData:GetAsync(plrkey)
if GetSaved then
Save1 = GetSaved[1]
Save2 = GetSaved[2]
Save3 = GetSaved[3]
else
local NumberForSaving = {Save1.Value, Save2.Value, Save3.Value}
StoreData:GetAsync(plrkey, NumberForSaving)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
StoreData:SetAsync("id_"..plr.UserId, {plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value})
end)
They are both serverscripts inside of serverscriptservice.
Is “EvolutionPoints” a string, and integer, or something else? And why are you referencing it with brackets instead of referencing it the same way you did with XP and Level?
1error I see in my game I can’t know if this works but hey here’s what I can see.
local DSS = game:GetService("DataStoreService")
local MDS = DSS:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local EP = Instance.new("IntValue")
EP.Name = "Evolution Points"
EP.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local data
local data2
local data3
local success, errormessage = pcall(function()
data = MDS:GetAsync(plr.UserId.."-Evolution Points")
data2 = MDS:GetAsync(plr.UserId.."-XP")
data3 = MDS:GetAsync(plr.UserId.."-Level")
end)
if success then
EP.Value = data
XP.Value = data2
Level.Value = data3
else
print("Failed!")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local success, errormessage = pcall(function()
MDS:SetAsync("id_"..plr.UserId, plr.leaderstats["Evolution Points"].Value, plr.leaderstats.XP.Value, plr.leaderstats.Level.Value)
end)
if success then
print("Success!")
else
print("Failed!")
warn(errormessage)
end
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreV1")
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
local EP = Instance.new("IntValue")
EP.Name = "Evolution Points"
EP.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Parent = leaderstats
local Level = Instance.new("IntValue")
Level.Name = "Level"
Level.Parent = leaderstats
local key = plr.UserId
local data
local success,msg = pcall(function()
data = DataStore:GetAsync(key)
end)
if data then
print("Data Loaded")
EP.Value = data.E
XP.Value = data.X
Level.Value = data.L
else
print("Error")
warn(msg)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local key = player.UserId
local data = {
E = player.leaderstats["Evolution Points"].Value,
X = player.leaderstats.XP.Value,
L = player.leaderstats.Level.Value,
}
local success,msg = pcall(function()
DataStore:SetAsync(key,data)
end)
if success then
print("Data Saved!")
end
end)