Hello, I’ve got a script that respawns a car when I click a button. This works great, but I want the car to only respawn if it’s the VehicleSeat is not occupied. I’m new to scripting, and I can’t seem to figure out how to fit this into the script, does anybody have a solution?
local cd = script.Parent.ClickDetector
local car = script.Parent.Parent.Car
local button = script.Parent
local debounce = false
car.Parent = game.ServerStorage
cd.MouseClick:Connect(function()
button.BrickColor = BrickColor.new("Black")
wait(15)
button.BrickColor = BrickColor.new("Magenta")
end)
cd.MouseClick:Connect(function()
if not debounce then
debounce = true
workspace.Cars.Jeeps.Jeep:Destroy()
print("Respawned Jeep")
local newcar = car:Clone()
newcar.Name = ("Jeep")
newcar.Parent = game.Workspace.Cars.Jeeps
wait(15)
debounce = false
end
end)
The script is in a button, which is in a model with the car that gets spawned, and the model is in a folder with the existing car which gets deleted and replaced with a car of the same name every time the button is clicked. the folder is then inside another folder, which is in the workspace.
VehicleSeats in vehicles have a neat little object property called “occupant”.
When a player sits in a vehicle seat, that property will be set to the players humanoid, and when the player gets out of the seat, the property will be empty (or nil)
We can use this property in the seat to check if the player isnt sitting in the car.
if not car.VehicleSeat.Occupant then
-- Respawn vehicle
end
Thanks, I didn’t know you could do this. Managed to fit this into the script, but it doesn’t seem to respawn the car even If I’m not in the seat. Could you look this over and see how I could fix my issue?
local cd = script.Parent.ClickDetector
local car = script.Parent.Parent.Car
local button = script.Parent
local debounce = false
local occupied = script.Parent.Parent.Parent.Jeep.VehicleSeat.Occupant
car.Parent = game.ServerStorage
cd.MouseClick:Connect(function()
button.BrickColor = BrickColor.new("Black")
wait(15)
button.BrickColor = BrickColor.new("Magenta")
end)
cd.MouseClick:Connect(function()
if not debounce and occupied then
debounce = true
workspace.Cars.Jeeps.Jeep:Destroy()
print("Respawned Jeep")
local newcar = car:Clone()
newcar.Name = ("Jeep")
newcar.Parent = game.Workspace.Cars.Jeeps
wait(15)
debounce = false
end
end)
Heres your main problem.
You have a variable that is set to the value of the occupant of the seat. If you know the basics of variables, they do not update themselves. Which means your occupant variable wont change. So its always nil (assuming that the seat isn’t occupied upon running the game)
A simple fix that i would do is to replace the occupant variable with a VehicleSeat variable, and then you can get the occupant of the vehicle seat within your function or your if statement line
local vehicleSeat = script.Parent.Parent.Parent.Jeep.VehicleSeat
if not debounce and not vehicleSeat.Occupant then
I’ve had a brief idea of what’s been happening for the last few versions of the script, but now I’m really confused. The first time I try respawn the car while sitting in the seat, it doesn’t respawn as it should, but when I respawn it and try to do the same for the second time it while in the seat, it does. I’ve looked over the script a million times and can’t find an error, and no error message pops up either, do you see any issues here?
local cd = script.Parent.ClickDetector
local car = script.Parent.Parent.Car
local button = script.Parent
local debounce = false
local vehicleSeat = script.Parent.Parent.Parent.Jeep.VehicleSeat
car.Parent = game.ServerStorage
cd.MouseClick:Connect(function()
if not debounce and not vehicleSeat.Occupant then
debounce = true
workspace.Cars.Jeeps.Jeep:Destroy()
print("Respawned Jeep")
local newcar = car:Clone()
newcar.Name = ("Jeep")
newcar.Parent = game.Workspace.Cars.Jeeps
wait(5)
debounce = false
end
end)
cd.MouseClick:Connect(function()
button.BrickColor = BrickColor.new("Black")
wait(5)
button.BrickColor = BrickColor.new("Magenta")
end)
Thank you for all your help so far. I’m very sorry if I’m causing any inconvenience for you.
I think i know the issue. And again its to do with the seat variable.
I forgot to mention this, but once you’ve respawned the vehicle, the variable for the vehicle seat is still set to the old vehicle seat that got destroyed from the old car.
So all we have to do to fix this is to set the vehicleSeat variable to the vehicle seat in the new car that has been cloned from the old one in the respawn function.
if not debounce and not vehicleSeat.Occupant then
debounce = true
workspace.Cars.Jeeps.Jeep:Destroy()
print("Respawned Jeep")
local newcar = car:Clone()
vehicleSeat = newcar.VehicleSeat -- reset the variable for the vehicleSeat to the new car’s vehicle seat
newcar.Name = ("Jeep")
newcar.Parent = game.Workspace.Cars.Jeeps
wait(5)
debounce = false
end