So I’m a little confused about why I got an error with my script. I was trying to make a system that only takes away money if a player has enough to purchase an upgrade (for a tycoon), but the error is saying ServerScriptService.MainScript:49: attempt to perform arithmetic (sub) on number and nil
. Here’s the part of the script that I’m referencing on the erroring line (the reference that’s erroring is the dictionary on the first line given, and the if statement is the line at the end.):
local buttonPrices = {
StartTycoonButton,startButtonBought = 1000000,false
}
--For every button, do
for i, v in pairs(workspaceChildren) do
if v:IsA("Model") and string.find(v.Name,"Tycoon") ~= nil then
local buttonsModelChildren = v.Buttons:GetChildren()
for i, v in pairs (buttonsModelChildren) do
--If button is touched, perform actions for that specific button
v.Touched:Connect(function(partThatTouched)
--If it's the start tycoon button, then
if string.find(v.Name,"StartTycoonButton") ~= nil then
if partThatTouched.Parent.Humanoid ~= nil then
local playerThatTouchedId = game.Players:GetUserIdFromNameAsync(partThatTouched.Parent.Name)
if playerThatTouchedId == v.Parent.Parent.Misc.OwnerDoor.MainBlock.CurrentOwnerId.Value then
--Subtract money if player has enough money to purchase
local playerThatOwnsTycoonString = game.Players:GetNameFromUserIdAsync(v.Parent.Parent.Misc.OwnerDoor.MainBlock.CurrentOwnerId.Value)
if game.Players[playerThatOwnsTycoonString].leaderstats.Money.Value - buttonPrices[StartTycoonButton] >=0 then