Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Regen Dilemma, can it be fixed?

 I have a new vehicle regen that is designed  not to regen the vehicle if the vehicle is in use. However, when the vehicle is legitimately regenned the vehicle disintegrates to its most basic parts. How can I fix this so the vehicle can be regenned to its original structure rather than fall apart? Here is a copy of the regen script.
local Vehicle = script.Parent.Vehicle:Clone()

for i, v in pairs(Vehicle:GetChildren()) do
	if v:IsA("Script") then
		v.Disabled = true
	end
end
local CurrentVehicle = script.Parent.Vehicle

local Regen = script.Parent.Regen
local ClickDetector = Regen.ClickDetector
local Display = Regen.Display.Label

local InUse = false
local Debounce = false

function UpdateUse()
	Display.Text = Debounce and "REGENERATING" or InUse and "VEHICLE IS IN USE" or "REGEN"
end

function HandleVehicle(vehicle)
	InUse = false
	local seats = {}
	
	local function CheckUse()
		local used = false
		for i, v in pairs(seats) do
			if v then
				used = true
			end
		end
		InUse = used
		UpdateUse()
	end
	
	local function GetAllSeats(vehicle)
		for i, v in pairs(vehicle:GetChildren()) do
			if v:IsA("VehicleSeat") or v:IsA("Seat") then
				table.insert(seats, false)
				local id = #seats
				v.ChildAdded:connect(function(child)
					if child.Name == "SeatWeld" then
						seats[id] = true
						CheckUse()
					end
				end)
				v.ChildRemoved:connect(function(child)
					if child.Name == "SeatWeld" then
						seats[id] = false
						CheckUse()
					end
				end)
			elseif v:IsA("Model") then
				GetAllSeats(v)
			end
		end
	end
	GetAllSeats(vehicle)
end

HandleVehicle(script.Parent.Vehicle)

ClickDetector.MouseClick:connect(function()
	if not InUse and not Debounce then
		Debounce = true
		UpdateUse()
		CurrentVehicle:Destroy()
		CurrentVehicle = Vehicle:Clone()
		CurrentVehicle.Parent = Workspace
		for i, v in pairs(CurrentVehicle:GetChildren()) do
			if v:IsA("Script") then
				Delay(0.1, function()
					v.Disabled = false
				end)
			end
		end
		HandleVehicle(CurrentVehicle)
		wait(5)		
		Debounce = false
		UpdateUse()
	end
end)
 Currently, I use the old person299 regen. Besides the fact that the vehicle can be regened right out from under you, the regen works great. Here is a copy of the person299 script if it can help.
--Edited Button Regen Script by Person299
system = script.Parent
local c  = system:GetChildren()
for i = 1,#c do
a = c[i]
if a.Name ~= "person299" then
if a.Name ~= "put in only 1 model" then
model = a
end
end
end
backup = model:Clone()
regen = system:findFirstChild("put in only 1 model")
function checkRegen()
if regen.Value == 1 then
if(model.Parent == system) then
model:Remove()
wait(1)
end
model = backup:Clone()
model.Parent = system
model:MakeJoints()
end
end
regen.Changed:connect(checkRegen)

2 answers

✓ Accepted answer

The joints for the vehicle is not connecting when regened.

To make it connect, use the :MakeJoints() method (as shown on Person299's script on line 22) on your model.

ClickDetector.MouseClick:connect(function()
    if not InUse and not Debounce then
        Debounce = true
        UpdateUse()
        CurrentVehicle:Destroy()
        CurrentVehicle = Vehicle:Clone()
        CurrentVehicle.Parent = Workspace
	CurrentVehicle:MakeJoints()				-- CurrentVehicle connects its joints at this point
        for i, v in pairs(CurrentVehicle:GetChildren()) do
            if v:IsA("Script") then
                Delay(0.1, function()
                    v.Disabled = false
                end)
            end
        end
        HandleVehicle(CurrentVehicle)
        wait(5)     
        Debounce = false
        UpdateUse()
    end
end)

2 comments

0

Thank you Redbullusa, It works! In my small scale tests it functions just as well as Person299, time to implement it full scale! I’m also a little annoying because I was so close to fixing it myself. Before I posted this help request I made the very same modification but on line 69 as opposed to line 70 :S

0

You're very welcome. C:

You can't compare "and" to a string, it doesn't know what the string means.

This discussion is preserved as it was. New answers, comments, and votes are closed.