What I am trying to accomplish is, on touch, a GUI is replicated into the player GUI from server storage. I have done this without FE but I am looking to find out how to do it with FE.
On touch how would I get from the character to the player? And then their player GUI?
Would i have to use Remote Functions/Events or will something more simple like:
function onTouch(Humanoid) h=~nil then GetPlayerFromCharacter
will something like that do?
You would have to use a remote function. The server side would handle the part where it detects the touch:
1 | local RemoteEvent = game.ReplicatedStorage.RemoteEvent --name this whatever you'd like, and place it in the Replicated Storage, as both the Client and Server can access it. |
2 |
3 | function onTouch(hit) |
4 | if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) then |
5 | RemoteEvent:FireClient(game.Players:GetPlayerFromCharacter(hit.Parent)) |
6 | end |
7 | end |
8 |
9 | script.Parent.Touched:connect(onTouch) |
This fires the event that lets the client do whatever is connected to the event. This connection would be defined in a LocalScript. You can place it in the StarterPlayerScripts or somewhere similar where localscripts can run:
01 | local RemoteEvent = game.ReplicatedStorage.RemoteEvent --again, make sure this name matches up to the RemoteEvent you're firing in the server script. |
02 | local player = game.Players.LocalPlayer |
03 |
04 | function ShowGui() |
05 | local gui = game.ReplicatedStorage.GUI_NAME:clone() --whatever you called it |
06 | if ( not player.PlayerGui:FindFirstChild(gui.Name)) then |
07 | gui.Parent = player.PlayerGui |
08 | end |
09 | end |
10 |
11 | RemoteEvent.OnClientEvent:connect(ShowGui) |
You don't need to use remote events. You can do everything in the server script.
01 | -- In a server script within the part being touched |
02 | local part = script.Parent |
03 |
04 | part.Touched:connect( function (otherPart) |
05 | if otherPart.Parent then |
06 | local player = game.Players:GetPlayerFromCharacter(otherPart.Parent) |
07 | if player and not player.PlayerGui:FindFirstChild( "ScreenGui" ) then |
08 | game.ServerStorage.ScreenGui:Clone().Parent = player.PlayerGui |
09 | end |
10 | end |
11 | end ) |