Is this how I would efficiently do a "player owned block" system?

Note, my game has a building system and an owner string value in each block.

I was thinking to put click detectors every block, see if their mouse hovered on the block for more than around 2 seconds, and make a menu pop up for whoever owns the block.

To make the on hovered function though, I’ll just do it from the server script. But would I have to disconnect its function afterwards?

How about no. Just use Mouse.Target or raycasts (preferred) to identify the block that the player is looking at. Putting a detector in EVERY SINGLE BLOCK is peak redundancy.

How about using attributes? It’s as simple as

block:SetAttribute('Owner', player.Name) --set block owner

local ownerName = block:GetAttribute'Owner' --get block owner, nil if no owner

No need for additional instances.

How about no once again. Since this is a feature for clients, you do it on the client. This helps relieve some of the processing burdens from the server so that it can do what actually matters.

2 Likes

Is correct in the information they’ve provided you, here’s a little bit of pseudo-code to get you started.

local Game = game
local Players = Game:GetService("Players")
local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()

local function OnMouseMove()
	local Target = Mouse.Target --Get mouse's target.
	if not Target then return end --If target is 'nil' then return out of the function.
	task.wait(1) --Wait for length of 'hover' duration.
	if Target ~= Mouse.Target then return end --Compare target from one second prior with the current target.
	local OwnerName = Target:GetAttribute("OwnerName") --Brick owner's username, useful for displaying on a UI.
	local OwnerId = Target:GetAttribute("OwnerId") --Brick owner's user ID, useful for datastores, fetching avatar thumbnails etc.
end

Mouse.Move:Connect(OnMouseMove)

The attributes mentioned would need to be set when the brick is initially placed.

1 Like

I wish I can solution this too, but I’ll definitely be doing the mouse move thing.