Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
1
Answered by
DanzLua 2879 Moderation Voter Community Moderator
5 years ago

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)
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 Louix27626 83 — 5y
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 DanzLua 2879 — 5y
0
Thank you so much <3 Louix27626 83 — 5y
Ad

Answer this question