Trouble with remote functions

I’m making a renting system, and right now, I’m trying to make is so that a player can’t rent a room if another player is already renting it. When a player rents a room, their userId is stored in a table with a value of the door. It looks like this:

local doorTable = {
123456789 = door
}

When they run out of time, they get removed from the table. To check if anyone is renting the room, I’m using table.find to find the index of the door. Here is the script:

local function isRoomBeingUsed(player, doorObject)
	local beingUsed = table.find(doorTable, doorObject)
	return beingUsed
end
	
fireIsRoomUsed.OnServerInvoke = isRoomBeingUsed()

Here’s the local script that prompts the player to rent the room:

tech60.MouseButton1Click:Connect(function()
	print("tech60 activated")
	local beingUsed = fireIsRoomUsed:InvokeServer(tech)
	if not beingUsed then
		MarketplaceService:PromptProductPurchase(LocalPlayer,1353158718)
		print("prompted")
	else
		print("being used")
	end
end)
-- there are a lot more, I just repeat this for each room

For some reason, I’ll click the door to rent the room, and it’ll print “tech60 activated”, but I won’t print “prompted” or “being used”. What’s the issue here?

You’ve ran the function and set its return to OnServerInvoke instead of setting itself to it.

fireIsRoomUsed.OnServerInvoke = isRoomBeingUsed() -- <-- Adding a pair of parenthesis is a call already

Remove the pair of parenthesis.

Now, it will work, except for it doesn’t stop the player from renting if the room is already being rented. How do I fix this?

The table library does not work on dictionaries. Only arrays.

This is also invalid formatting, and regardless whether it is properly formatted, this is a dictionary:

local doorTable = {
123456789 = door -- Wrap your index with brackets [] unless it is a string with no whitespace.
}

Instead, do this:

local doorTable = {
	[door] = { -- You can use objects as keys.
		plrId = 1234567890,
	},
}

And for the isRoomBeingUsed

local function isRoomBeingUsed(player, doorObject)
	return doorTable[doorObject] -- Access the value by object.
end
1 Like

You need to add an object value or something into each room and set it to the player, and then, detect if the value is being used or not. If not, allow them to rent.

Wait sorry I’m a little confused. I might’ve been wrong about how I said it looked like up there. Here’s how it is set:

productFunctions[1353158718] = function(receipt, player)
	playerTable[player.UserId] = os.time()
	cooldownTable[player.UserId] = 10
	doorTable[player.UserId] = tech
	receiptProcessed:FireClient(player,tech)
	print("tech90")
end

Would table.find be wrong for this case?

Yes, you just made a dictionary, something table.find can’t find stuff in.

You’re adding a numeral index that is more than the length of the array, making it a dictionary.

Oh :frowning: So will it just be best to do what @SubtotalAnd8185 said? To set an object value of each door to the player while renting? I guess this would also make it so I would’nt have to use remote functions.

I have low confidence in object values, however you can still use tables. Just like the example code I provided, you can simply use the door objects themselves as the key.

If the room is vacant, remove the door from the table.

If a room has been rented, add the door to the table as a key, and the value can be the ID of the player, or a table with the player ID (and some other stuff you want to add.

1 Like

Ok, I think I’ll try that first, and if it doesn’t work, I’ll try using object values.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.