When I get out of the car and try going on it or try touching it, the car pushes me back.
robloxapp-20221119-1954277.wmv (4.8 MB)
Scripts:
CarService:
local CarService = {}
local StartUpSound = Instance.new("Sound")
function CarService.new(Car)
print("Created Car")
-- Car functions that this script will use
local CarFunctions = require(script.Parent.CarFunctions)
CarFunctions:CarAnchore(Car,true) -- Makes the Car Anchored
local Seat = Car.Body.VehicleSeat
local EnterProx = CarFunctions:CreatePrompt(Car) -- Creates the E button
-- When the player presses e next to the car they get in it
EnterProx.Triggered:Connect(function(player)
StartUpSound:Play()
if Seat.Occupant == nil then
local CarInfo = Instance.new("Folder",player) -- This will handle the car's info such as if the car should stop or not
CarInfo.Name = "CarInfo"
local Stop = Instance.new("BoolValue",CarInfo)
Stop.Name = "Stop"
EnterProx.Enabled = false -- Disables the prompt
print("triggered")
local character = player.Character or player.CharacterAdded:Wait()
if character:WaitForChild("Humanoid") then
-- Forces the player's humanoid to sit in the car
Seat:Sit(character:WaitForChild("Humanoid"))
CarFunctions:SetCharPhysics(character,false) -- Sets the collision groups with the character and car
CarFunctions:CarAnchore(Car,false) -- Unanchores the car
Car.PrimaryPart:SetNetworkOwner(player) -- Sets the network owner of the car to the player
require(game.ReplicatedStorage.Drive).DriveInit(player,Car) -- These are the drive functions on the client to make the car drive
local Platform = Car.PrimaryPart
local CylFL = Platform.CylindricalFL
local CylFR = Platform.CylindricalFR
local CylRL = Platform.CylindricalRL
local CylRR = Platform.CylindricalRR
-- When the "Stop" bool value changes it stops the whole car
-- It sets the network to auto the character collision groups back to normal and the car's anchore
Stop.Changed:Connect(function()
if Stop.Value == true then
Seat.ThrottleFloat = 0
Seat.Throttle = 0
Seat.Steer = 0
Seat.SteerFloat = 0
CylFL.MotorMaxTorque = 0
CylFR.MotorMaxTorque = 0
CylRR.MotorMaxTorque = 0
CylRL.MotorMaxTorque = 0
CylFL.AngularVelocity = 0
CylFR.AngularVelocity = 0
CylRR.AngularVelocity = 0
CylRL.AngularVelocity = 0
Car.PrimaryPart:SetNetworkOwnershipAuto()
CarFunctions:SetCharPhysics(character,true)
CarFunctions:CarAnchore(Car,true)
EnterProx.Enabled = true
print("Enter prox")
end
end)
-- When the player gets out of the car stop the car
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if Seat.Occupant == nil then
Stop.Value = true
end
end)
print("Started Car")
else
return
end
else
print("return")
return
end
end)
end
return CarService
CarFunctions
local PhysicsService = game:GetService("PhysicsService")
-- CG stands for Collision Group
local DefaultCG = "Default"
local CharacterCG = "Character"
local Functions = {}
-- Creates a proximity prompt to enter the car
function Functions:CreatePrompt(Car)
local Chasis = Car.Chasis
local Platform = Chasis.Platform
local Attachment = Instance.new("Attachment", Platform)
local ProximityPrompt = Instance.new("ProximityPrompt", Attachment)
ProximityPrompt.Name = "Enter"
ProximityPrompt.ActionText = "Enter Car (E)"
ProximityPrompt.RequiresLineOfSight = false
return ProximityPrompt
end
function Functions:SetCharPhysics(character,Collide)
local Group = (Collide and DefaultCG or CharacterCG) -- If the player is getting in it will set them to not collide else it would set it to collide
for i, part in pairs(character:GetDescendants()) do
if part:IsA("BasePart") then
part.Massless = not Collide
PhysicsService:SetPartCollisionGroup(part,Group) -- Sets the part to the "Group"
end
end
end
-- Sets every part in the car anchored to the value passed
function Functions:CarAnchore(Car,Anchore)
for i, v in pairs(Car:GetDescendants()) do
if v:IsA("BasePart") then
v.Anchored = Anchore
end
end
end
return Functions
Drive
local RunService = game:GetService("RunService")
local Drive = {}
local DriftSound = Instance.new("Sound")
local DriveSound = Instance.new("Sound")
function Drive.DriveInit(player,Car)
local CarInfo = player:WaitForChild("CarInfo")
local Stop = CarInfo:WaitForChild("Stop")
-- Variables
local seat = Car.Body.VehicleSeat
local Platform = Car.Chasis.Platform
local Connection
local maxSteerAngle = 35
local steer = 0
local AttachmentFL = Platform.AttachmentFL
local AttachmentFR = Platform.AttachmentFR
Connection = RunService.Heartbeat:Connect(function(dt)
-- This is the steering if the seat's steerfloat then that means the car is turning
local steerGoal = -seat.SteerFloat * maxSteerAngle
steer = steer + (steerGoal - steer) * math.min(dt * seat.TurnSpeed, 1) local CylFL = Platform.CylindricalFL
local CylFR = Platform.CylindricalFR
local CylRL = Platform.CylindricalRL
local CylRR = Platform.CylindricalRR
-- Sets both front wheels orientation to "steer"
AttachmentFL.Orientation = Vector3.new(0,steer,-90)
AttachmentFR.Orientation = Vector3.new(0,steer,-90)
local throttle = seat.ThrottleFloat
local torque = seat.Torque
local speed = seat.MaxSpeed * throttle
if throttle == 0 then
-- Brake function
if DriveSound.IsPlaying then
else
DriveSound:Play()
end
else
if DriveSound.IsPlaying then
DriveSound:Stop()
end
end
-- Sets the attachments torque to the torque of the car
CylFL.MotorMaxTorque = torque
CylFR.MotorMaxTorque = torque
CylRR.MotorMaxTorque = torque
CylRL.MotorMaxTorque = torque
-- Sets the speed of the attachments to move the car
-- the speed is the car's throttle right now * the max speed it can go
CylFL.AngularVelocity = speed
CylFR.AngularVelocity = -speed
CylRR.AngularVelocity = -speed
CylRL.AngularVelocity = speed
end)
-- Client code on if the Stop value is switched to true
-- This fires when the player gets out of the car
Stop:GetPropertyChangedSignal("Value"):Connect(function()
if Stop.Value == true then
Car.Chasis.Platform.Attachment.Enter.Enabled = true
Connection:Disconnect() -- Disconnects the driving function
end
end)
end
return Drive