Editing Dictionary inside a Module by using a Function within same module error

Hey so when a player enters my game, I save the following data into a dict called local storage = {}

The data that is saved is

local tab = {
		name = player.Name,
		userId = player.UserId,
		coins = 300
	}

Now I have a EditStorage function which changes the coins value

>  function module:EditStorage(id,Table,EditType,Amount)
> 	if id == nil or Table == nil or EditType == nil then return end
> 	for i,v in ipairs(storage) do
> 		if v.userId == id then
> 			if EditType == "Set" then
> 				v.Table = Amount
> 				warn("Set")
> 			elseif EditType == "Increment" then
> 				v.Table = v.Table + Amount
> 				return v.Table
> 			end
> 		end
> 	end
> end

When I run this store:EditStorage(id,"coins","Increment",200) It does print 500 (meaning it did properly increment) However when it does this function again it prints 500 again, meaning that the table “coins” is not being updated from this function.

What I want to have happen

I want the storage for that player id to update the coin value, at the minute its only changing for the function and isn’t saving properly. If any addition Information is needed then let me know!

THE STUFF BEING PASSED AS PARAMETERS INTO EDITSTORAGE

Id = player id
Table = the specific var the player wants edited. In this scenario is it “coins”
EditType = what type of operation you want done, via incrementing or setting
Amount = Integer, the value

1 Like

When you access the dictionary value try doing it as
v[Table] = ...
instead of
v.Table = ...

Tried it doesn’t work, I genuinely don’t understand why this issue is occuring (30 chars)

bump I’ve tried a lot of fix attempts I’m assuming this is not possible?

EDIT: Issue has been resolved

For anyone who has a similar issue in the future the problem I was having is the fact that I wasn’t returning the entire data, I was only returning a field. The Fixed Function below >

function module:EditStorage(id,Table,EditType,Amount)
if id == nil or Table == nil or EditType == nil then return end
for _,data in ipairs(storage) do
	if data.userId == id then
		if EditType == "Set" then
			data.Table = Amount
			warn("Set")
		elseif EditType == "Increment" then
			Amount = data[Table] + Amount
			data[Table] = Amount
			return data
		end
	end
end
1 Like