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

How to prevent an elevator to create error when 2 player push on the button at the same time?

I wanted to make an elevator, it works perfectly fine but, when 2 players push at the same time on the button from the level 1 and the level 2 for example, the elevator get stuck at the bottom or the top

I can't figure out where does the issue is from here's the script

local Pressed = false
script.Parent.ClickDetector.MouseClick:connect(function()
	repeat wait(1) until
	script.Parent.Parent.Parent.Parent.Elevator.Position.Y >= 17.5 -- maximum height
	or
	script.Parent.Parent.Parent.Parent.Elevator.Position.Y <= -57 -- minimum height
	wait(3) --supposed to be a cooldown before the elevator can react to the next call
	if not Pressed then
		Pressed = true
			
			repeat script.Parent.Parent.Parent.Parent.Elevator.Position = script.Parent.Parent.Parent.Parent.Elevator.Position + Vector3.new(0,0.2,0)
				wait(0.01)	
			until script.Parent.Parent.Parent.Parent.Elevator.Position.Y >= 17.5  --inverted with -57 for the second button
			
			repeat script.Parent.Parent.Parent.Parent.Elevator.Position = script.Parent.Parent.Parent.Parent.Elevator.Position - Vector3.new(0,0.2,0)
				wait(0.01)	
			until script.Parent.Parent.Parent.Parent.Elevator.Position.Y <= -57 --inverted too
		Pressed = false
	end
end)

This script is situatued into a button, there's 2 of them, the second have exactly the same code exept for the value

Thank you in advance.

1 answer

✓ Accepted answer

We can create a debounce with a BoolValue that will have to be located outside of these scripts

So let's assume that these button are located inside a model and along with them, there is a BoolValue instance with them.

local db=script.Parent.Parent.BoolValue --path to boolvalue
local Pressed = false

script.Parent.ClickDetector.MouseClick:connect(function()
    if db.Value==false then db.Value=true
        repeat wait(1) until
        script.Parent.Parent.Parent.Parent.Elevator.Position.Y >= 17.5 -- maximum height
        or
        script.Parent.Parent.Parent.Parent.Elevator.Position.Y <= -57 -- minimum height
        wait(3) --supposed to be a cooldown before the elevator can react to the next call
        if not Pressed then
            Pressed = true

                repeat script.Parent.Parent.Parent.Parent.Elevator.Position = script.Parent.Parent.Parent.Parent.Elevator.Position + Vector3.new(0,0.2,0)
                    wait(0.01)  
                until script.Parent.Parent.Parent.Parent.Elevator.Position.Y >= 17.5  --inverted with -57 for the second button
    
                repeat script.Parent.Parent.Parent.Parent.Elevator.Position = script.Parent.Parent.Parent.Parent.Elevator.Position - Vector3.new(0,0.2,0)
                    wait(0.01)  
                until script.Parent.Parent.Parent.Parent.Elevator.Position.Y <= -57 --inverted too
            Pressed = false
        end
        db=false
    end
end)

3 comments

0

It work even better but when 2 players spam the button 1 and 2 the elevator get stuck again I have no idea from where does this thing come from

0

@Louix27626 Then make it so that these two buttons check towards the same BoolValue. Like the entire shaft's buttons check at the same boolvalue

0

Thank you so much <3

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