While trying to cycle through Players using a for loop and then update their leaderstats using UpdateAsync every two minutes, I've gotten this through the output;
--[[
Player1
00:51:53.080 - Unable to cast value to function
00:51:53.081 - Stack Begin
00:51:53.081 - Script 'ServerScriptService.DataStore', Line 15 - global updateDataStore
00:51:53.082 - Script 'ServerScriptService.DataStore', Line 23
00:51:53.083 - Stack End
--]]
The DataStore script reads:
DataStore = game:GetService("DataStoreService"):GetDataStore("Main")
game.Players.PlayerAdded:connect(function(player)
player:WaitForDataReady()
wait(2)
local key = "U_" .. player.UserId
local gold = player.leaderstats.Gold
gold.Value = DataStore:GetAsync(key)
end)
function updateDataStore(player)
local key = "User_" .. player.UserId
local gold = player.leaderstats.Gold
DataStore:UpdateAsync(key, gold.Value)
end
while true do
wait(120)
for i, v in pairs(game.Players:GetChildren()) do
print(v)
updateDataStore(v)
end
end
game.Players.PlayerRemoved:connect(function(player)
player:WaitForDataReady()
wait(2)
updateDataStore(player)
end)
So since it printed (Player1), I'd have thought that the players name would be an acceptable value to input into the updateDataStore function, but apparently not. What is it I'm not understanding or have done wrong? Thank you for any help!

1 comment
That makes sense, thank you! On another note, I'm still having trouble getting it to work. Would it be best to post another question or update this one?