So I would like to know if I can make a GUI open only for 2 people because I’m making a trading system and I would like to know how to make the GUI only open for 2 people so they can trade
Get the server to fire a remote event to the players and have a local script connect to it and open the guis.
how do i do the local script par i understand ho to fire the event but not the second part
Create a local script somewhere where it can run and connect to the event:
-- example
RemoteEvent.OnClientEvent:Connect(function()
TradeGui.Visible = true -- make the gui visible
end)
but how will it ONLY open for 2 players
LocalScripts only operate for you. They don’t affect anyone else. Which is why I said fire the event to both of the players so their scripts can run.
-- server script
RemoteEvent:FireClient(Player1)
RemoteEvent:FireClient(Player2)
but the players in my game are not going to be name player 1 and 2
I know that, it was an example. You don’t send their names you send their Instance.
RemoteEvent.OnServerEvent:Connect(function(player) -- roblox passes the player who fired the event
RemoteEvent2:FireClient(player) -- fire the remote for the player
end)
No when he said player1
he meant that’s a variable that is referencing the player object: game.Players.WhateverTheNameOfThePlayerIs
For example what you can do is on the server script
local Bob = game.Players:FindFirstChild("Bob");
RemoteEvent:FireClient(Bob, "OpenGui")
Which would find the player named Bob and send “OpenGui”. From there you could set up on the local script
RemoteEvent.OnClientEvent:Connect(function (Event)
if (Event == "OpenGui") then
-- code to open gui
end
end)
Read the pages that @HugeCoolboy2007 linked
On mobile so sorry for weird formatting
You would replace those with something such as:
game.Players.playerUsername_123
I am pretty sure it’s if Event == “OpenGui” then. I think that syntax is in JavaScript.
You can use that in Luau as well.
You would use a remote event and fire it to a client.
If you don’t know how to do this follow these steps:
- Create a RemoteEvent inside ReplicatedStorage.
- Create a LocalScript.
- Put this inside your LocalScript:
local remote = game.ReplicatedStorage.RemoteEvent
local plr = game.Players.LocalPlayer
remote.OnClientEvent:Connect(function(gui)
local gui = plr.PlayerGui:FindFirstChild(gui)
if gui then
-- do stuff
end
end)
- Parent your LocalScript to StarterPlayerScripts.
- Inside your script you’ll want to fire that remote like this:
local remote = game.ReplicatedStorage.RemoteEvent
remote:FireServer(game.Players.playerusername, 'guiname')
This will only work if your script that your using to fire the remote is a server script.
Nah you can if (condition)
in Lua and Luau, and it’s a habit that has carried over from C# and JavaScript