How would you make a ui appear for a random person that's sitting at a table?

hello! im trying to make a UI appear for a random player that is sitting at the table but i have no idea how to approach this. each player has a sittingAttribute. i appreciate any feedback guys, thank u so much!

my module so far:

local module = {}

function module.popupUI()
	for _, plr in ipairs(game.Players:GetPlayers()) do
		local plrChar = plr.Character
		
		if plrChar:GetAttribute("sitAttribute") == true then
			print(plr.Name)
		end
	end
end

return module

Since GetPlayers() returns an array, you can index at random from 1 to the length of it.
** From there, you can create your own array of players who have the target attribute

local PlayerService = game:GetService("Players")

local function getRandomPlayer() : Player
  local players = PlayerService:GetPlayers()
  return players[math.random(1, #players)]
end
1 Like

hey man,
thanks a million for the help.

how would i make it so that if the players sit attribute is true, then to like make the ui appear for a random player?

im trying to get the players that are sat at the table basically. :slight_smile:

function getRandomPlr()
	local plrs = game:GetService("Players"):GetPlayers()
	for plrName, plr in ipairs(game.Players:GetPlayers()) do
local plrChar = plr.Character
		if plrChar:GetAttribute("sitAttribute") == true then
			return plrs[math.random(1, #plrs)]
		end
	end
end

function module.popupUI()
local UI = game.StarterGui.pickupPlateUI
end

return module

You would wanna change the players sit attribute upon touching the part in this instance a “chair”. Once the player touches the seat part you should change player attribute to true.

1 Like

hey man thanks for the response.

i have the sitting attribute set up so that when they sit down it sets to true so thats fine but i only want to get the players that have their sit attribute set to true :slight_smile:

Could be a long shott cause I’m not on a computer to test it out rn but maybe check to see if the script is getting plr character and the sit attributes value first.

local plrchar = blah blah blah
If plrchar then
print(“plr”)
If plrChar:GetAttribute(“sitAttribute”) == true then
print(“true”)

1 Like

hey man,

i got something going!! so basically this works but its enabling the GUI for everyone rather than one person. i want it only for 1 person in the whole server yk!

local module = {}

function getRandomPlr()
	wait(5)
	local plrs = game:GetService("Players"):GetPlayers()
	for plrName, plr in ipairs(game.Players:GetPlayers()) do
local plrChar = plr.Character
		if plrChar:GetAttribute("sitAttribute") == true then
			return plrs[math.random(1, #plrs)]
		end
	end
end

function module.popupUI(randomPlr)
	randomPlr = getRandomPlr()
local UI = randomPlr.PlayerGui.pickupPlateUI
UI.Enabled = true


end

return module

You would want something to add the player to the table upon sitting and remove when leaving

1 Like

Found something similar that worked with someone else.

1 Like

So you want to get 1 random player that is sitting at the table? I’m a bit confused lol

1 Like
local function GetRandomPlayer()
    local PlayersSitting = {}
    for i, v in pairs(game:GetService("Players"):GetPlayers()) do
        local char = v.Character
        if char:GetAttribute("sitAttribute") == true then
            table.insert(PlayersSitting, v)
        end
    end
    if #PlayersSitting == 1 then
        return PlayersSitting[1]
    else
        return PlayersSitting[math.random(1, #PlayersSitting)]
    end
end

This should work. Sorry for the bad formatting, I typed all of this on mobile.

2 Likes

that’s exactly it man! sorry if i confused u. like breaking point basically!

this is what i got (thanks to a lot of ppl):

local module = {}


function getRandomPlr()
	wait(4)
	local PlayersSitting = {}
	for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
		local char = plr.Character
		if char:GetAttribute("sitAttribute") == true then
			table.insert(PlayersSitting, plr)
		end
	end
	if #PlayersSitting == 1 then
		return PlayersSitting[1]
	else
		return PlayersSitting[math.random(1, #PlayersSitting)]
	end
end

function module.popupUI(randomPlr)
	randomPlr = getRandomPlr()
local UI = randomPlr.PlayerGui.pickupPlateUI
UI.Enabled = true


end

return module

1 Like

Has this been fixed already or are you still looking for a solution? If so, could you tell us what’s not working correctly?

1 Like

bro formatting isnt important at all. its the functionality!!! :slight_smile:

tysm for replying i adjusted my script to your answer but unfortunately it still enables the UI for every player. is it to do with the for loop maybe?

			table.insert(PlayersSitting, plr)
		return PlayersSitting[math.random(1, #PlayersSitting)]

it returns a whole bunch of players instead of picking just one?

still looking for a solution bc i have no idea what im doing unfortunately.

this white box should appear for one random person that’s sitting at the table.

i linked a video here to show whats going on for different player POVs. :smiley:

1 Like

yes i have something like that set up already ! :smiley:

yes that is quite similar. i bookmarked it for future work so thank u so much for this man !!

im just trying to get one player for the moment. :slight_smile:

Whats the code that fires module.popupUI()?

I realized you’re running this script on the client, which may seem to make sense as you’re trying to enable an UI, though, part of the script should be on the server. The reason is, if this is a LocalScript (or a ModuleScript required by a LocalScript) the getRandomPlr() function will run for every player in the game. If there are 7 players, it’ll get 7 random players. We don’t want that, do we? So to fix this, you should put that function on the server and send a RemoteEvent to the Client. That way, the game will pick 1 random player. A lot of yapping so let’s put this into code:

Module:

function module.popupUI(randomPlr)
	local UI = randomPlr.PlayerGui.pickupPlateUI
	UI.Enabled = true
end

Server:

function getRandomPlr()
	local PlayersSitting = {}
	for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
		local char = plr.Character
		if char:GetAttribute("sitAttribute") == true then
			table.insert(PlayersSitting, plr)
		end
	end
	if #PlayersSitting == 1 then
		return PlayersSitting[1]
	else
		return PlayersSitting[math.random(1, #PlayersSitting)]
	end
end

function Start()
	wait(4)
	Remote:FireClient(getRandomPlr())
end

Start() -- Put this wherever you need the thing to start

Client:

local player = game.Players.LocalPlayer

Remote.OnClientEvent:Connect(function() -- Only the "RandomPlayer" will receive this Remote
	module.popupUI(player) 
end)

You’ll have to change some things a bit, but this is just to give a general idea. Hope I helped!

1 Like

hey man, thank you so much for ur help. i suspected it was to do w remote events. still inexperienced tho. :pensive:

im getting this error in the server script:
this line:

	return PlayersSitting[math.random(1, #PlayersSitting)]

image

script:

local remote = game.ReplicatedStorage:WaitForChild("pickupPlateEvent")

function getRandomPlr()
	local plrs = game.Players:GetPlayers()
	local PlayersSitting = {}
	
	if #plrs > 1 then
		for i, plr in pairs(game:GetService("Players"):GetPlayers()) do
			local char = plr.Character
			if char:GetAttribute("sitAttribute") == true then
				table.insert(PlayersSitting, plr)
			end
		end
	end
	return PlayersSitting[math.random(1, #PlayersSitting)]
	end


function Start()
	wait(4)
	remote:FireClient(getRandomPlr())
end

if #game.Players:GetPlayers() > 1 then
	Start()
end