Where do I keep my local script?

Where do I keep my local script? While testing alone I kept in playerGui, but then when the local script says thisgui.Visible = true it is visible for all players (I would use local player but if the script is in startgui then it is running for all players. Also, if I duplicate the plane, then lots and lots local scripts with different names will have to be stored. I am very confused, where do I keep it and how do I not make one for every single plane?

You can use a module script to handle all the UI’s with 1 script

Ah, ty. How do I do that exactly?

https://www.youtube.com/results?search_query=roblox+how+to+use+module+scripts

I’m sorry but what does the Local Script do? I can’t exactly tell what you are looking for.

So the local script takes all the players input to drive the vehicle as well as activate the UI buttons if they are on mobile.

Ah, ty. If the module script takes care of the UI, how do I personalise each local script to be relevant to exactly that vehicle. Because let’s say they buy the plane, I cannot just duplicate the vehicle because then both local scripts will reference the same one right?

Think of module scripts as “containers” for your code that you can reuse in multiple places. For example, if you have an object that gets copied multiple times (like a mob), it’s a lot easier to have 1 main script control the behavior instead of multiple copied scripts inside each mob.

Ex:

--local script in StarterPlayerScripts
--Services
local Player = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Variables
local player = Player.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")
local uis = playerGui.UiFolder

--Dependencies
local moduleScriptYouMade = ReplicatedStorage.moduleScriptYouMade

--Functions
for _, ui in pairs(uis) do
    moduleScriptYouMade.FunctionYouMade(ui)
end

--The variable you made in the module script will print 5!
--You can also change its value like a regular variable
print(moduleScriptYouMade.VariableCount)
--Module script in ReplicatedStorage (I like to put mine in a folder called ModuleScript to organize them)
--Think of module scripts like a table, that you can put things into
local moduleScriptYouMade = {}

--You can also put variables inside the module scripts that act like a public variable!
moduleScriptYouMade.VariableCount = 5

--This makes a function inside the module script that you can use in other places!
function moduleScriptYouMade.FunctionYouMade(ui)
    print(ui.Name)
end

return moduleScriptYouMade
2 Likes

Something really helpful for that is a programming paradigm called Object Oriented programming. It might take a bit to write my own tutorial for it, so I’ll link you some great ones made by other programmers. It’s GREAT to learn, especially in game dev. Helps keep your code clean and organized into individual containers that are applied to objects you want to use.

The tutorial on the lua page:
https://www.lua.org/pil/16.html

1 Like

So what I’m assuming is if the player is on mobile and when they get on vehicle a GUI would pop out for them?

Just use a script where it detects if they are on mobile, if they are on mobile it’ll set a bool value in the player to true telling the game that the player is on mobile and the GUI would pop out every time a player hops in a car or something.

Read the post again. He’s asking how to organize the code so he doesn’t need to copy and paste multiple scripts in different objects.

Oh epic, ty. So using Object Oriented Programming, would it then be possible for there to be a single local script that picks up on each time any player gives inputs and then passes those parameters along with the players name to the module script, which will then check if there are in a plane and if they are then move the plane object that was created for that player by matching the players name to the plane their sitting on?

Could you give me a bit more context on what you’re trying to achieve with the plane?

So I made a plane, but I’ve realised if another player buys the plane and I duplicate it in, then both plane models will have the same name. So when the local script takes input when the player moves forwards

local cframe = CFrame.new(3.5, 0, 0)
plane1.CFrame = plane1.CFrame:ToWorldSpace(foroffsetCFrame)

then since both planes are called plane 1 and the local script refers to plane1 and the remote event replicates it to the server, then both plane1’s will start flying forward even though only one is being driven. So my only solution so far is that I would have to have eg 20 planes with then 20local scripts each saying plane2, plane3, plane4 etc. with all the local scripts being kept in starter gui, as idrk where else to keep them. I think I am going about this completely wrong though. If you need I can add the local and server scripts code into here

1 Like

Yeah OOP can help with that, but there are other methods that can solve your issue too. (I’d recommend OOP though for this case).

1 Like