I’m trying to make a housing system in a game I’m working on where when a player buys a house they are added to a table of all players that have bought a house so they can’t buy a second house.
When I test my script in game it returns this error: ServerScriptService.HouseOwnersTable:12: attempt to call a nil value - Server - HouseOwnersTable:12
I know it’s a problem with how I’m doing the table because I had the script print(plr) at the top to make sure it was properly receiving that info so I know the “nil value” in question is the table
I’ve looked everywhere and even watched videos about how to do tables for beginners but still can’t find a solution
here’s the code:
local homeownerslist = {}
game.ReplicatedStorage.HouseTable.Event:Connect(function(plr, isitownedinfo)
print(plr)
if isitownedinfo == true then
homeownerslist.remove(homeownerslist, plr)
return
end
if isitownedinfo == false then
homeownerslist.insert(homeownerslist,plr)
print("added plr")
return
end
end)
I may be incorrect, but I believe if you write homeownerslist.remove() you don’t need to pass home ownerslist again because you already “passed” it. Like the reply above mine, try doing table.insert(homeownerslist, plr).
Tables do not inherently contain functions like insert or remove. Those functions belong to the global table library. Therefore, you cannot do this:
local someTable = {}
someTable.insert() -- This will cause an error
someTable.remove() -- This will cause an error
Instead, you should pass the table as the first argument to the library functions like this:
local someTable = {}
table.insert(someTable, something)
table.remove(someTable, somethingPosition)
To remove an item by its value rather than its index, you first need to find its position using table.find():
local someTable = {"apple", "banana", "orange"}
local position = table.find(someTable, "banana")
if position then
table.remove(someTable, position)
end