Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Car not working when spawned in using script?

Asked by 3 years ago

When its in the workspace, it works perfectly. When I spawn it in using a script, it doesn't work. I have the car in the serverstorage, in a "Cars" folder.

--DriveScript
local ServerStorage = game:GetService("ServerStorage")
local Car = ServerStorage.Cars.Car
local vehicleSeat = Car.Base.VehicleSeat

local motorFR = Car.WheelFR:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorFL = Car.WheelFL:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorBR = Car.WheelBR:FindFirstChildWhichIsA("CylindricalConstraint", true)
local motorBL = Car.WheelBL:FindFirstChildWhichIsA("CylindricalConstraint", true)

local springFR = Car.WheelFR:FindFirstChildWhichIsA("SpringConstraint", true)
local springFL = Car.WheelFL:FindFirstChildWhichIsA("SpringConstraint", true)
local springBR = Car.WheelBR:FindFirstChildWhichIsA("SpringConstraint", true)
local springBL = Car.WheelBL:FindFirstChildWhichIsA("SpringConstraint", true)

local wheelHingeR = Car.WheelFR:FindFirstChildWhichIsA("HingeConstraint", true)
local wheelHingeL = Car.WheelFL:FindFirstChildWhichIsA("HingeConstraint", true)


-- TUNING VALUES
----------------------------------------
-- Factor of torque applied to get the wheels spinning
-- Larger number generally means faster acceleration
local TORQUE = 8000 

-- Factor of torque applied to change the wheel direction
-- Larger number generally means faster braking
local BRAKING_TORQUE = 8000

-- Max angle the wheels will reach when turning
-- Higher number means sharper turning, but too high means the wheels might hit the car base
local MAX_TURN_ANGLE = 30

-- Car max speed
local MAX_SPEED = 140
----------------------------------------


-- HELPER FUNCTIONS
----------------------------------------
-- Set the "MotorMaxTorque" property on all of the CylindricalConstraint motors
local function setMotorTorque(torque)
    motorFR.MotorMaxTorque = torque
    motorFL.MotorMaxTorque = torque
    motorBR.MotorMaxTorque = torque
    motorBL.MotorMaxTorque = torque
end

-- Set the "AngularVelocity" property on all of the CylindricalConstraint motors
local function setMotorVelocity(vel)
    motorFL.AngularVelocity = vel
    motorBL.AngularVelocity = vel
    -- Motors on the right side are facing the opposite direction, so negative velocity must be used
    motorFR.AngularVelocity = -vel
    motorBR.AngularVelocity = -vel
end

-- Calculate the average linear velocity of the car based on the rate at which all wheels are spinning
local function getAverageVelocity()
    local vFR = -motorFR.Attachment1.WorldAxis:Dot(motorFR.Attachment1.Parent.RotVelocity)
    local vRR = -motorBR.Attachment1.WorldAxis:Dot(motorBR.Attachment1.Parent.RotVelocity)
    local vFL = motorFL.Attachment1.WorldAxis:Dot(motorFL.Attachment1.Parent.RotVelocity)
    local vRL = motorBL.Attachment1.WorldAxis:Dot(motorBL.Attachment1.Parent.RotVelocity)
    return 0.25 * ( vFR + vFL + vRR + vRL )
end


-- DRIVE LOOP
----------------------------------------
while true do

    -- Input values taken from the VehicleSeat
    local steerFloat = vehicleSeat.SteerFloat  -- Forward and backward direction, between -1 and 1
    local throttle = vehicleSeat.ThrottleFloat  -- Left and right direction, between -1 and 1

    -- Convert "steerFloat" to an angle for the HingeConstraint servos
    local turnAngle = steerFloat * MAX_TURN_ANGLE
    wheelHingeR.TargetAngle = turnAngle
    wheelHingeL.TargetAngle = turnAngle

    -- Apply torque to the CylindricalConstraint motors depending on our throttle input and the current speed of the car
    local currentVel = getAverageVelocity()
    local targetVel = 0
    local motorTorque = 0

    -- Idling
    if math.abs(throttle) < 0.1 then
        motorTorque = 100

    -- Accelerating
    elseif math.abs(throttle * currentVel) > 0 then
        -- Reduce torque with speed (if torque was constant, there would be a jerk reaching the target velocity)
        -- This also produces a reduction in speed when turning
        local r = math.abs(currentVel) / MAX_SPEED
        -- Torque should be more sensitive to input at low throttle than high, so square the "throttle" value
        motorTorque = math.exp( - 3 * r * r ) * TORQUE * throttle * throttle
        targetVel = math.sign(throttle) * 10000  -- Arbitrary large number

    -- Braking
    else
        motorTorque = BRAKING_TORQUE * throttle * throttle
    end

    -- Use helper functions to apply torque and target velocity to all motors
    setMotorTorque(motorTorque)
    setMotorVelocity(targetVel)
    wait()
end


Next Script

--CarSpawner
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SpawnCarEvent = ReplicatedStorage:WaitForChild("SpawnCar")
local DeleteCarEvent = ReplicatedStorage:WaitForChild("DeleteCar")

SpawnCarEvent.OnServerEvent:Connect(function(player, carName)
    local Car = ServerStorage:FindFirstChild("Cars"):FindFirstChild(carName)
    if Car then
        local clonedCar = Car:Clone()
        clonedCar.Name = player.Name .. 'sCar'
        clonedCar.Parent = game.Workspace
        clonedCar:MoveTo(player.Character.HumanoidRootPart.Position + player.Character.HumanoidRootPart.CFrame.LookVector * 15)
    end
end)

DeleteCarEvent.OnServerEvent:Connect(function(player, Car)
    if Car then
        Car:Remove()
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
    if Car then
        Car:Remove()
    end
end)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local Car = game.Workspace:FindFirstChild(player.Name .. 'sCar')
        if Car then
            Car:Remove()
        end
    end)
end)

I can provide the delete and call scripts if needed.

0
Errors errors please are there any errors in the output? those are 130 lines and there might be more reasons. Please check the output imKirda 4491 — 3y
0
Ok, I figured out why. The script spawns the car into Workspace, with the name "playername'sCar'. For example, mcslimemansCar. How would I make 003 local Car = ServerStorage.Cars.Car into something like 003 local Car = workspace.playernamesCar ? mcslimeman 37 — 3y
0
Also, there are no errors in the output mcslimeman 37 — 3y
0
mhm you need to get the player and then do this: workspace[player.Name.."'Cat"]. It will look for your car then. imKirda 4491 — 3y
View all comments (2 more)
0
ok, how would i get the player? mcslimeman 37 — 3y
0
The scripts are on a normal script. mcslimeman 37 — 3y

Answer this question