Setting an attribute does not actually change my attribute value?

Hi, I’m working on a tycoon framework to easily build on. Adding money doesn’t work and removing money does, the difference between them is one operator.

The value doesn’t get set anywhere else in my script, only in these module functions.

  10:22:28.057  Selling part for 200  -  Server - Sell on touch:6
  10:22:28.057  Adding money to tycoon  -  Server - Money:42
  10:22:28.057  x: 6000 y: 200  -  Server - Money:43
  10:22:28.058  Total: 6200  -  Server - Money:45

My module script for money value’s and editting (the cash worth of each tycoon is set with an attribute and synced with a loop to the leaderstats)

local money = {}

local function tycoonTable()
	local tycoon = {}
	for _, v in pairs(workspace.Tycoons:GetChildren()) do
		table.insert(tycoon, v)
	end
	return tycoon
end

money.getMoney = function(x: Part | Folder)
	local blacklist = {
		["Workspace"] = true,
		["game"] = true,
		["Tycoons"] = true,
		["nil"] = true
	}

	while x do
		if blacklist[x.Name] then
			return nil
		end
		if table.find(tycoonTable(), x) then
			return x:GetAttribute("Cash")
		end
		x = x.Parent
	end
	return nil -- found nothing
end


money.removeMoney = function(x: any, y: any)
	if money.getMoney(x) then
		x:SetAttribute("Cash", money.getMoney(x) - y)
	else
		warn("Failed to get the player's money")
	end
end

money.addMoney = function(x: any, y: any)
	if money.getMoney(x) then
		print("Adding money to tycoon")
		print("x: "..money.getMoney(x), "y: "..y)
		x:SetAttribute("Cash", (money.getMoney(x) + y)) -- this is not a breakpoint this is my breaking point.
		print("Total: "..x:GetAttribute("Cash")) -- actually gets added but also doesn't
	else
		warn("Failed to get the player's money")
	end
end

return money

A video of what I mean with it not actually adding / getting reset.

If you require further information let me know.

1 Like

are trying to increase it from a local script or a normal script ?

The only difference between your print (which shows the correct total) and your values not saving is that for 1 youre using getattribute / setattribute but for the other youre using your own getmoney function. For that reason I suspect the issue is within that function, maybe its getting the money of the wrong part? Try printing the names of the parts being parsed into getmoney to make sure its the same one youre setting the attribute of.

1 Like

Blind guess

money.addMoney = function(x: any, y: any)
    local currentMoney = money.getMoney(x)
    if currentMoney ~= nil then
        x:SetAttribute("Cash", currentMoney + y)
    else
        warn("Failed to get the player's money")
    end
end

To make sure this part is working as intended.

My entire system is server sided

1 Like

Same thing happens with no output as expected

Start putting prints down of them values to show where you’re going wrong.
Also in realtime follow it in the Studio, checking the Attribute.
part:SetAttribute(“Example”, nil) – Removes the attribute
A nil in there is like a delete command.

These parts in this folder … is every one set up with the Attribute correctly…

1 Like

I tried logging output on the getMoney function and the output was as the following:

  19:27:32.761   ▼  {
                    [1] = SELL,
                    [2] = Dropper,
                    [3] = DROP,
                    [4] = Dropper,
                    [5] = Purchases,
                    [6] = Spiderman
                 }  -  Server - Money:29

Reference:
image

I don’t see how it could index the wrong Cash value, since it just returns the tycoon that is an descendant from.

My editted logging:

money.getMoney = function(x: Part | Folder)
	-- Returns the money of the tycoon
	-- returns nil if it's not a tycoon / not found
	local blacklist = {
		["Workspace"] = true,
		["game"] = true,
		["Tycoons"] = true,
		["nil"] = true
	}
	
	local indexes = {}

	while x do
		if blacklist[x.Name] then
			return nil
		end
		table.insert(indexes, x)
		if table.find(tycoonTable(), x) then
			print(indexes)
			return x:GetAttribute("Cash")
		end
		x = x.Parent
	end
	return nil -- found nothing
end

I already did that in the original post.

The values don’t change in studio, or atleast, change back super fast.

Your money.addMoney function seems perfectly fine, but it’s the arguments that you pass that are incorrect.

When you called the money.removeMoney function, you passed the arguments tycoon and cost, which means you set the tycoon’s Cash attribute to the tycoon’s Cash attribute value minus the cost. But when you called the money.addMoney function, you passed the sell cube and the sell cube’s Worth attribute value as arguments, which means you set the sell cube’s Cash attribute to the tycoon’s Cash attribute value plus the sell cube’s Worth attribute value. To fix this problem, pass the tycoon as the first argument.

check if there is any other script trying to change the value in the same time

In the video it already shows me doing that.

0:41

change

_MONEY.addMoney(ItemInTheTycoon, ItemInTheTycoon:GetAttribute("Worth"))

to

_MONEY.addMoney(Tycoon, ItemInTheTycoon:GetAttribute("Worth"))
-- define Tycoon to be a reference to the tycoon

I think this part of the code is causing issues, could you replace this:

if table.find(tycoonTable(), x) then
	return x:GetAttribute("Cash")
end

with this?

if workspace.Tycoons:FindFirstChild(x) then -- you could do x.Name
	return x:GetAttribute("Cash")
end