How to end an if statement before it reaches the else statement?

Hello everyone, I’m scripting a vehicle spawn system, and inside the StarterCharacter is a boolean called “HasVehicle”. Basically, when the player spawns a vehicle it turns this value to true, so then if they try to spawn another vehicle they cannot. This is the structure:

if workspace[namedplayer].HasVehicle.Value == true then

				--how do i end it here before it goes to the else statement

			else

				sound:Play()

				gui.ScrollingFrame.Visible = false
				button.Visible = true

				local vehicletospawn = v.Vehicle.Value			
				local spawnedvehicle = vehicledir[vehicletospawn]:Clone()
				spawnedvehicle.Parent = game.Workspace
				spawnedvehicle:PivotTo(gui.Parent.Parent.SpawnPos.CFrame)

			end

I want to know how I can end the if statement if the value is set to true.

Thanks in advanced!

2 Likes
if 6 > 5 then
    print("yayy")
    return
else
    print("well that's just sad")
end

just add return

1 Like

If I understand your question:

local a = true

if a then
--//a is true, so this code block will execute, the else statement only executes if this condition is NOT true
else
--//The else clause only executes if the expression above is not evaluated to true
end

If you only need to execute some code if a player’s HasVehicle value is false, then change your condition testing if the value is false instead of true.

1 Like

Thanks, I also realized I must’ve removed the portion of the script that changed that value to true in the first place. A bit of a dunce moment :sweat_smile:

1 Like
if not workspace[namedplayer].HasVehicle.Value then
	sound:Play()
	gui.ScrollingFrame.Visible = false
	button.Visible = true
	local vehicletospawn = v.Vehicle.Value			
	local spawnedvehicle = vehicledir[vehicletospawn]:Clone()
	spawnedvehicle.Parent = game.Workspace
	spawnedvehicle:PivotTo(gui.Parent.Parent.SpawnPos.CFrame)
end