How to send a variable as a parameter in a function in a.. Can't really explain

What I’m trying to do is this:

local function UpdateData(CurrentData, NewData)
	print(CurrentData, NewData) -- CurrentData is fine, NewData is nil
	CurrentData = NewData
	return CurrentData
end

local SaveData = {
		Time = Player.leaderstats.Time.Value,
		DonatedBux = Player.leaderstats.DonatedBux.Value,
		NameTags = {
			Robux10 = Player:GetAttribute("Ten"),
			Robux100 = Player:GetAttribute("Hundred"),
			Robux1000 = Player:GetAttribute("Thousand"),
			Robux10000 = Player:GetAttribute("TenThousand"),
			Robux100000 = Player:GetAttribute("HundredThousand"),
			OneMillion = Player:GetAttribute("OneMillion")
		}
	}

local success, errormessage = pcall(function()
	DataStore:UpdateAsync(Player.UserId, UpdateData(SaveData)) -- SaveData isn't going through
end)

I’m trying to make it so that I can send two things in as parameters, one by default for updateasync is “CurrentData” but I also want to pass in the SaveData variable. But it’s not working, how do I do this?

Edit: There’s a second parameter in the callback function for updateasync which is datastoreinfo or whatever, if that affects passing it in as a parameter.

I have… no idea what you are trying to do here. I can tell you that based on this post, you’re supposed to pass in the function UpdateData instead of calling it, and the only argument that will be passed into that function is the current data. From there you can return the new data and it will save.

can’t you just do this?

local success, errormessage = pcall(function()
	DataStore:UpdateAsync(Player.UserId, function(CurrentData)
	    local NewData = --CurrentData / SaveData?
	    return NewData
    end)
end)

Or this

local function UpdateData(CurrentData)
	CurrentData = --What is new data?
	return CurrentData
end

local success, errormessage = pcall(function()
	DataStore:UpdateAsync(Player.UserId, UpdateData)
end)
1 Like

Calling UpdateData in its current state will return nothing, hence why the values are nil. p.s. that is NOT a proper way of doing that.
Try something like this:

local function UpdateData(CurrentData, NewData)
    local Ok, Err = pcall(function(...): ()
        DataStore:UpdateAsync(Player.UserId, --[[ Add the new save here ]] );
    end
    assert(Ok, Err);
end

Currently you are passing the return value of the function instead of the function itself. Also, the function you are passing won’t accept additional parameters, it will automatically get passed the old value and the datastore params, so you will need to amend it a little to accept these arguments.

local SaveData = {
		Time = Player.leaderstats.Time.Value,
		DonatedBux = Player.leaderstats.DonatedBux.Value,
		NameTags = {
			Robux10 = Player:GetAttribute("Ten"),
			Robux100 = Player:GetAttribute("Hundred"),
			Robux1000 = Player:GetAttribute("Thousand"),
			Robux10000 = Player:GetAttribute("TenThousand"),
			Robux100000 = Player:GetAttribute("HundredThousand"),
			OneMillion = Player:GetAttribute("OneMillion")
		}
	}

local function UpdateData(CurrentData, params)
	print(CurrentData, params)
	CurrentData = SaveData
	return CurrentData
end

local success, errormessage = pcall(function()
	DataStore:UpdateAsync(Player.UserId, UpdateData)
end)
Or you could combine things into:

This would be more dynamic, quicker, and easier to call, it uses setasync instead because the transformation function didn’t really do anything to the current data except replace it, which is what setasync does anyway.

local function UpdateData(player)
    local SaveData = {
		Time = Player.leaderstats.Time.Value,
		DonatedBux = player.leaderstats.DonatedBux.Value,
		NameTags = {
			Robux10 = Player:GetAttribute("Ten"),
			Robux100 = player:GetAttribute("Hundred"),
			Robux1000 = player:GetAttribute("Thousand"),
			Robux10000 = player:GetAttribute("TenThousand"),
			Robux100000 = player:GetAttribute("HundredThousand"),
			OneMillion = player:GetAttribute("OneMillion")
		}
	}
    return pcall(function()
        DataStore:SetAsync(player.UserId, SaveData)
    end
end

local success, error = UpdateData(Player)

Thanks man I didn’t see that in my script, I’ll do this and I’ll see if it works but pretty sure it will.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.