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

How can I fix this regen button lock system?

local descendants = script.Parent.Parent["DU-300 Heavy SPG"]:GetDescendants()

while true do
	wait()
	for index, descendant in pairs(descendants) do
		if descendant:IsA("Seat") or descendant:IsA("VehicleSeat") and descendant.Occupant ~= nil then
			script.Parent.ClickDetector.MaxActivationDistance = 0
			script.Parent.BrickColor = BrickColor.new("Bright red")
		else
			script.Parent.ClickDetector.MaxActivationDistance = 100
			script.Parent.BrickColor = BrickColor.new("Steel blue")
		end
	end
end

Here, i'm trying to create a regen lock system.

So when someone is seated in the vehicle, the regen button is locked and unclickable. There are multiple seats in the vehicle, and when i sit in one seat, the regen button rapidly switches from locked to unlocked. How can i fix this?

1 answer

✓ Accepted answer

The problem you're having is caused by the while loop. It is running infinitely and checking all seats over and over with some being occupied and some being unoccupied which is switching the lock on and off. Instead of using a loop, it would be wise to use an event that checks when a new player occupies or leaves a seat. I wrote a script that applies this method here:

local model = script.Parent.Parent["DU-300 Heavy SPG"]
local descendants = model:GetDescendants() -- Get the car model's descendants
local lockRegen = false -- Keeps track of the state of the regen button (locked/unlocked)
local currentOccupants = 0 -- Keeps track of current amount of players seated

for i = 1, #descendants do
	
	local descendant = descendants[i] -- Current descendant in the loop
	
	if descendant:IsA("Seat") or descendant:IsA("VehicleSeat") then
		descendant:GetPropertyChangedSignal("Occupant"):Connect(function() -- Fires when the occupant property of the seat changes
			local occupant = descendant.Occupant -- Used to check if the occupant is nil or not
			if lockRegen == false then
				lockRegen = true
				currentOccupants = 1
				script.Parent.ClickDetector.MaxActivationDistance = 0
				script.Parent.BrickColor = BrickColor.new("Bright red")
			else
				if occupant == nil then
					if currentOccupants == 1 then
						lockRegen = false
						currentOccupants = 0
						script.Parent.ClickDetector.MaxActivationDistance = 0
						script.Parent.BrickColor = BrickColor.new("Steel blue")
					else
						currentOccupants = currentOccupants - 1
					end
				else
					currentOccupants = currentOccupants + 1
				end
			end
		end)
	end
end

If you have any questions or problems with this script, please let me know. I'd be happy to clarify or fix it.

Hope this helps!

1 comment

0

thank you so much, man!

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