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.
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
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”)
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
Found something similar that worked with someone else.
So you want to get 1 random player that is sitting at the table? I’m a bit confused lol
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
Has this been fixed already or are you still looking for a solution? If so, could you tell us what’s not working correctly?
bro formatting isnt important at all. its the functionality!!!
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.
yes i have something like that set up already !
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.
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!
hey man, thank you so much for ur help. i suspected it was to do w remote events. still inexperienced tho.
im getting this error in the server script:
this line:
return PlayersSitting[math.random(1, #PlayersSitting)]
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.
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
hey man, tysm for ur help. your answer was the closest thing to the solution so i marked it.
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