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.
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.
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
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…
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
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.