Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Unable to cast value to function when trying to update data in a DataStore with appropriate value?

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 answer

✓ Accepted answer

:UpdateAsync is for when you're trying to do something like increment (a "change by doing this...") type update.

Use :SetAsync to just set the new value:

function updateDataStore(player)
	local key = "User_" .. player.UserId
    local gold = player.leaderstats.Gold
    DataStore:SetAsync(key, gold.Value)
end

 

1 comment

0

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?

This discussion is preserved as it was. New answers, comments, and votes are closed.