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
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.
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.
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
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”)
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
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.
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
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!
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