Kalananti Game Coding Bootcamp - Driving Simulator Game (10-12)

Source Code

CarScript
local Car = script.Parent
local Seat = Car.VehicleSeat

local FrontLeft = Car.FrontLeft
local FrontRight = Car.FrontRight
local BackLeft = Car.BackLeft
local BackRight = Car.BackRight

local SteerAngle = 30
local Speed = 60

local GetMoney = false
local Player = nil

Seat:GetPropertyChangedSignal("Steer"):Connect(function()
	FrontLeft.PartB.SteeringConstraint.TargetAngle = SteerAngle * Seat.Steer
	FrontRight.PartB.SteeringConstraint.TargetAngle = SteerAngle * Seat.Steer
end)

Seat:GetPropertyChangedSignal("Throttle"):Connect(function()
	if Seat.Throttle ~= 0 then
		GetMoney = true
	else
		GetMoney = false
	end
	
	FrontLeft.Wheel.WheelConstraint.AngularVelocity = Speed * Seat.Throttle
	FrontRight.Wheel.WheelConstraint.AngularVelocity = Speed * -Seat.Throttle
	
	BackLeft.Wheel.WheelConstraint.AngularVelocity = Speed * Seat.Throttle
	BackRight.Wheel.WheelConstraint.AngularVelocity = Speed * -Seat.Throttle
end)

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Humanoid = Seat.Occupant
	
	if Humanoid then
		Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
	else
		Player = nil
	end
end)

while wait(1) do
	if GetMoney == true and Player ~= nil then
		local PlayerGui = Player.PlayerGui
		local ScreenGui = PlayerGui.ScreenGui
		local Leaderstats = Player:WaitForChild("leaderstats")
		local Money = Leaderstats:WaitForChild("Money")
		
		Money.Value += 1
		
		if Money.Value >= 100 and ScreenGui.Enabled == false then
			local TextLabel = ScreenGui.TextLabel	

			TextLabel.Position = UDim2.fromScale(0.5, 1.5)

			ScreenGui.Enabled = true

			TextLabel:TweenPosition(
				UDim2.fromScale(0.5, 0.5),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Back,
				0.5,
				false
			)
		end
	end
end
Leaderstats
local Players = game:GetService("Players")

function PlayerAdded(Player)
	local Leaderstats = Instance.new("Folder")
	Leaderstats.Name = "leaderstats"
	Leaderstats.Parent = Player

	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = Leaderstats
end

Players.PlayerAdded:Connect(PlayerAdded)

Starter Project

Starter Project - Driving Simulator Game.rbxl (108 KB)