Trying to make a vehicle spawning gui to where if you push a button it will clone a gui from serverstorage into the player who pressed the button's player gui, then once they select a vehicle it will find their position and spawn a vehicle on them.
Well yes, since you are doing it with a button(gui) you can keep parenting until get to the parent of the starter gui, since starter gui becomes playergui where its parent is the player folder. Here is an example: Let's say the button is in a screen gui and the screen gui is in the starter gui, you make a script in that button and start it with a variable like so:
local player = script.Parent.Parent.Parent.Parent
And like that you got the player folder
Basic Info
Firstly, you should be writing your own code to show that you've made an attempt at this
I would instead have the VehicleGui in the StarterGui
and simply change its Enabled
property rather than perform the cloning but to answer your question...
Examples
Information about Hierarchy
Use RemoteEvent
s to make button activations affect the server
ServerScript
s in these examples would be in the ServerScriptService
LocalScript
s in these examples would be parented under the button which is clicked / activated.RemoteEvent
s in the ReplicatedStorage
named "CloneEvent" and "SpawnEvent" respectively.TextButton
that was activatedServerStorage
Clone a Gui into the Player's PlayerGui
Local Script
local remote = game:GetService("ReplicatedStorage"):WaitForChild("CloneEvent") local button = script.Parent button.Activated:Connect(function() remote:FireServer() end)
Simply fire the event when clicked
Server Script
local remote = game:GetService("ReplicatedStorage"):WaitForChild("CloneEvent") local vgui = game:GetService("ServerStorage"):WaitForChild("VehicleGui") remote.OnServerEvent:Connect(function(player) local pgui = player:WaitForChild("PlayerGui") if not pgui:FindFirstChild(vgui.Name) then local clone = vgui:Clone() clone.Parent = pgui end end)
Check if a "VehicleGui" is already in the PlayerGui
, if not, clone it and place it there
Spawn Vehicle at Player's Location
Local Script
local remote = game:GetService("ReplicatedStorage"):WaitForChild("SpawnEvent") local button = script.Parent button.Activated:Connect(function() remote:FireServer(button.Text) end)
Fires the remote assuming the selected car is based on a TextButton
's text
Server Script
local remote = game:GetService("ReplicatedStorage"):WaitForChild("SpawnEvent") local SS = game:GetService("ServerStorage") remote.OnServerEvent:Connect(function(player, car) local pgui = player:WaitForChild("PlayerGui") local vgui = pgui:FindFirstChild("VehicleGui") if vgui then vgui:Destroy() end local root = player.Character:WaitForChild("HumanoidRootPart") local vehicle = SS:WaitForChild(car):Clone() vehicle.Parent = workspace vehicle:SetPrimaryPartCFrame(root.CFrame + Vector3.new(0, 0, 0)) end)
First, remove the VehicleGui, then spawn the car to the player's location
SetPrimaryPartCFrame
is meant for Model
s only
The addition of a Vector3
is in case the spawned car should be offset from the player's location by a certain amount