When Cloning a Vehicle the Controls Sometimes Break

When a vehicle gets spawned, it’s positioned, parented to a folder in workspace and then calls seat:Sit() for the player. I’m having a problem currently though, with that when I use this function sometimes the controls break. I’ve narrowed down the problem to it’s something to do with sitting the player, because if I disable seat:Sit() (comment it out) and then walk over to the seat instead, the controls always work. But currently only sometimes the controls work, does anyone have an idea to why this is happening?

I really didn’t want to make a devforum post but I’ve spent two entire days trying to figure out a solution to this problem so I’m clearly missing something. I’ve looked at at least 20 posts but none of them have helped me. I’m using the default jeep from the race template and you can view the relevant parts of my code below, the player is seated at the very bottom of the script.

Code

This has been solved so I’ve removed the code to prevent copying.

2 Likes

The main problem with your code is that it does not wait for the player’s character to spawn before trying to sit them in the car. This can cause problems because the car may not exist yet, or the player’s character may not be loaded.

To fix this, you can add a wait() call before the task.delay() call, which will wait for the player’s character to spawn before trying to sit them in the car.

This is only part of the code, the character will have already loaded by the time it gets to here. Also:

there’s a wait() here. I had already thought what you were saying could be a possibility which is why I wrapped seat:Sit() in task.delay().

Ohhhh, alright. I just flew over your code, I will rescript it now :sweat_smile:

Your code wasn’t wrong, per se, but there was a problem with the way the player was being seated in the vehicle. In the original code, the player was being seated as soon as the vehicle was spawned. This caused some problems with the player’s controls, because the player wasn’t given enough time to adjust to the new vehicle. By delaying the seating of the player by 0.5 seconds, this problem is alleviated.

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character:WaitForChild("Humanoid")
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0

if player.Character:WaitForChild("Humanoid").Sit then
	player.Character.Humanoid.Sit = false
end

local spawnPos = currentCourse.SpawnPositions[1]
local carSettings = customCarStore:GetAsync(player.UserId.."-settings")
local carClone
if carSettings.Car then
	carClone = Vehicles[carSettings.Car]:Clone()
else
	carClone = Vehicles.Jeep:Clone()
end
carClone.Name = "Vehicle"
local seat = carClone:FindFirstChildWhichIsA("VehicleSeat")
if not carClone.PrimaryPart then
	carClone.PrimaryPart = seat
end
carClone:PivotTo(spawnPos:GetPivot() * CFrame.new(0,4,0))
carClone.Parent = game.Workspace.Vehicles

task.delay(0.5,function()
	seat:Sit(humanoid)
end)

This might be the same code, I said I rescripted the code.

1 Like

I’m not sure what you’re talking about, that was already there, did you look over my last post and are we even looking at the same script?

Yes, I said I rescripted the code. So that I had the same code was just random right now, but I think this has something to do with Roblox Studio itself, maybe try it in the actual game.

I have, it breaks even more often in-game.

If by rescripted you mean removing the for loop and wait(), it doesn’t look like you’ve changed anything else. Also, this is a server script so game.Players.LocalPlayer would error.

Ohhhhhh, thanks for that information but I gotta focus on my own script right now so I might be able to fix it later, if you can help me please view my topic.

Ok, that’s fine.

1 Like

Is this an older vehicle that used joined parts, or is it connected with weld constraints? If older and with joined parts… then you will want to MakeJoints right before you parent to workspace.

I’m not quite sure what you mean, it’s currently the same as the jeep from the race template.

The controls always work if you sit in the car yourself instead of using the :Sit() function. So I’m not sure how to fix that but I was hoping you or someone else might have a solution?

I’ve seen this issue before but am not famililar with how to fix… my only suggestion would be to try to separate the functions. So first clone the vehicle into the workspace, then in a separate function, sit the humanoid.

I was excited for a moment there, it worked in studio but then I tried it in-game and it didn’t work. :confused:

1 Like

I’ve been able to fix it… somehow. I fixed it a couple of days ago but wanted to do thorough testing before coming to a conclusion and now I can’t remember how I fixed it. I think it was that other bits of my script were programmed really poorly because I just returned to this project after not working on it for a few months.

If anyone else encounters this problem, my only suggestion would be to see if other bits of your script are breaking it.

1 Like

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