How would I go about this?

Ello! Need a little help.
Basically, I need to find the SelectedCar (which I already defined) and then get it and define it under “Car”. What’s confusing here is SelectedCar is the car model name, not the path. Now I tried to define SelectedCar with the path of the car, didn’t work. How would I have Car give the path of the selectedcar even though SelectedCar is a string?

Btw, the car is at the path game.SeverStorage.Car_Mustang. Making Car the path of the car wouldn’t work as there are mutliple cars.

script.Parent.MouseButton1Up:Connect(function(player)
	
	local SelectedCar = game.StarterGui["car spawn"].Main.VehicleInfo.Selected.Value
	local Car = SelectedCar
	local CloneCar = Car:Clone()
	local Position1 = game.Workspace["Civilian Spawn"].CarSpots.S1.Position
	local Position2 = game.Workspace["Civilian Spawn"].CarSpots.S2.Position
	local Position3 = game.Workspace["Civilian Spawn"].CarSpots.S3.Position
	local Position4 = game.Workspace["Civilian Spawn"].CarSpots.S4.Position
	local Position5 = game.Workspace["Civilian Spawn"].CarSpots.S5.Position
	local PossibleSpawns = {Position1, Position2, Position3, Position4, Position5}

	CloneCar.Parent = game.Workspace
	CloneCar.PrimaryPart = CloneCar.Primary
	CloneCar:SetPrimaryPartCFrame(CFrame.new(PossibleSpawns[math.random(1, #PossibleSpawns)]))
	
	game.ReplicatedStorage.PlayerSpawnedCar:FireServer(player)
	script.Parent.Parent.Enabled = false
end)

Thanks!

You can set a path as a value using an ObjectValue.

Solved one problem, caused another.
For some reason it can’t find my object in ServerStorage even though it is in Serverstorage.

image

I would need to see your full script. This doesn’t look like it has anything to do with the function you provided in the first post.

It’s a clearly defined path, no idea why it’s doing this.

script.Parent.MouseButton1Up:Connect(function(player)
	 script.Parent.Parent.Parent.VehicleInfo.Selected.Value = game.ServerStorage.Car_Mustang
end)

ServerStorage cannot be found by localscripts
u should move your object to ReplicatedStorage (if you call it via server and client)

Once again, fixed a problem and caused a problem.
Now it can’t clone the defined object.

script.Parent.MouseButton1Up:Connect(function(player)
	local SelectedCar = game.StarterGui["car spawn"].Main.VehicleInfo.Selected.Value
	local Car = SelectedCar
	local CloneCar = Car:Clone() -- Look here
	local Position1 = game.Workspace["Civilian Spawn"].CarSpots.S1.Position
	local Position2 = game.Workspace["Civilian Spawn"].CarSpots.S2.Position
	local Position3 = game.Workspace["Civilian Spawn"].CarSpots.S3.Position
	local Position4 = game.Workspace["Civilian Spawn"].CarSpots.S4.Position
	local Position5 = game.Workspace["Civilian Spawn"].CarSpots.S5.Position
	local PossibleSpawns = {Position1, Position2, Position3, Position4, Position5}

	CloneCar.Parent = game.Workspace
	CloneCar.PrimaryPart = CloneCar.Primary
	CloneCar:SetPrimaryPartCFrame(CFrame.new(PossibleSpawns[math.random(1, #PossibleSpawns)]))
	
	game.ReplicatedStorage.PlayerSpawnedCar:FireServer(player)
	script.Parent.Parent.Enabled = false
end)

You should be spawning the car on the server instead of the client. Currently, you are cloning and spawning the car on the client, which won’t replicate to other users and could cause some other issues.

Also, don’t use StarterGui to get GUI values, as that is static. Use the LocalPlayer’s PlayerGui and get the GUI and other objects you need there.

1 Like

Add an if statement. The error means the selected car is nil.

local Car = SelectedCar
if Car then
    local CloneCar = Car:Clone()
else
    print("Car not selected")
end

Thanks bro, fixed the spawning issue. Now, it’s not able to actually select the car which been one of the issues.


image

This script is being fired from the client.

Found a workaround, thanks though.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.