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

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

Before selecting a random player, ensure that #PlayersSitting is greater than zero.

1 Like

i tried your way and i couldnt get it eventually but i finally got it. thank u so much for ur help Will, i really appreciate it :slight_smile:

hey man, tysm for ur help. your answer was the closest thing to the solution so i marked it. :slight_smile:

this is what i had to do to the server 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

while true do
	if #game.Players:GetPlayers() > 1 then
		Start() -- Put this wherever you need the thing to start
		break
	end
	wait(3)
end

1 Like