I need to communicate from a regular script in serverscriptservice to a local script in starter gui. I dont have any experience with remote functions and events. im making a story game so i really need to know how to do this. any thoughts would help.
Yes. Multiple ways, you could use module scripts that can store info on what to run and call it from another script, or you could use a remote event. Remote events can be ran by something like this:
1 | local event = game.ReplicatedStorage.Event |
2 | event:FireServer() |
You can tell something to happen when this is activated by:
1 | local event = game.ReplicatedStorage.Event |
2 |
3 | event.OnServerEvent:Connect( function () |
4 | print ( "Event fired" ) |
5 | end ) |
Hope this helped!
Your only answer kinda really is RemoteEvents
. You could use ModuleScripts
but they are usually used for tables and must return one value.
You should try to learn on how they work by other people's posts and devforums, etc. The devhub can be confusing so I'll make an example for you.
1 | --Client script (script in gui) |
2 | local lplayer = game.Players.LocalPlayer --Grab the player |
3 | script.Parent.MouseButton 1 Click:Connect( function () --Let's say a play button is pressed |
4 | game.ReplicatedStorage.RemoteEvent:FireServer(lplayer) --Send the player as a parameter |
5 | end ) |
1 | --Serverscript |
2 | game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect( function (player, lplayer) |
3 | local TP = game:GetService( "TeleportService" ) |
4 | TP:Teleport( 1234567890 , lplayer) --teleport the player who clicked the button |
5 | end ) |