local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
-- Load data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Cash.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
warn("There was an error!") ------
error(errormessage) ------ These two are the lines I got the errormessage
end
end)
Hello, I am quite new to DataStores.
I am having trouble with DataStores. I have researched and tried many different methods of Saving Data, However, none work for me. API services were turned on. When I test this in the server and update my Cash Value, it does not save.
In my Output box it says "Unable to cast value to function". Please Help me!

8 comments
Just remove "error(errormessage" then
"error(errormessage)"*
Ok, thanks, I'll try that.
wait i don't think I wrote that correctly, its not because of the errormessage
I've updated the quetion
You have to tell us which line errored.
Ok
Other than those lines, I am unsure which lines have errored