Vehicle GUI Spawner

Hello! I have this game, R&N Driving. Currently I use buttons the player has to press to spawn a vehicle.

With my new update coming out, I want to have a GUI to spawn my vehicles.
I would like to do this in a ScreenGUI.

How would I go about doing this?

1 Like

Whenever a player clicks the button In a gui take it out of wherever you have it say its ServerStorage (Clone it) Then position it to the head/HumanoidRootPart to the player, U might have to take away the position my 3 so it doesn’t glitch the player.

First you need to be familiar with how UI buttons function. If you need to touch on this you would want to reference the following objects and analyze their functions.

ImageButton
TextButton

You’re going to want to insert buttons into your ScreenGui. If you want to be organized, you might want to put in a Frame first and put those buttons inside the frame so that they are together. After you have positioned and sized the buttons accordingly, you can get to the scripting part.
If you’re looking for an easier way to organize and size your buttons, I’d recommend using UIListLayouts or UIGridLayouts

Now that you have your buttons inserted into the ScreenGui, you will need to make a local script. This can either be in the ScreenGui itself or in StarterPlayer>StarterPlayerScripts (I recommend this method).

Now you can start the scripting.

1.) Define the players GUI and find your ScreenGui within it.

local PlayerGui = game.Players.LocalPlayer:WaitForChild('PlayerGui')
local Screen = PlayerGui:WaitForChild('ScreenGuiName')

2.) Once you have your Screen, find your buttons and set variables for them.

local PlayerGui = game.Players.LocalPlayer:WaitForChild('PlayerGui')
local Screen = PlayerGui:FindFirstChild(ScreenGuiName)

local carButton_1 = Screen:FindFirstChild("buttonName")
local carButton_2 = Screen:FindFirstChild("buttonName")

3.) If you looked at the button functions earlier, or are already familiar with them, then you’ll know that ImageButtons and TextButtons have an Event for being clicked called MouseButton1Click()

You’ll attach this to the button variables you have defined. To spawn a car like you did originally, you’ll most likely need to call a RemoteEvent or RemoteFunction to get the server to spawn the car like it was when you were using the wall buttons.

This is what the code should look like once you have attached your buttons to MouseButton1Click()

local PlayerGui = game.Players.LocalPlayer:WaitForChild('PlayerGui')
local Screen = PlayerGui:FindFirstChild('ScreenGuiName')

local carButton_1 = Screen:FindFirstChild("buttonName")
local carButton_2 = Screen:FindFirstChild("buttonName")

carButton_1.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.SpawnCar:FireServer('CarName')
end)
carButton_2.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.SpawnCar:FireServer('CarName')
end)

If you aren’t familiar with RemoteEvents and RemoteFunctions I would highly recommend learning about those as they are used extremely often.

There are a lot more efficient ways to go about this, but this is a simple introduction to User Interface programming.