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

why isnt delayed being set to false again?

Asked by 2 years ago
Edited 2 years ago
local rep = game.ReplicatedStorage
local mouseDetection = rep.MouseDetection
local UIS = game:GetService("UserInputService")
local delayed = false
local delayTime = 0
UIS.InputBegan:Connect(function(input)
    if delayed == false then
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            delayed = true
            mouseDetection:FireServer()
        elseif delayed == true then
            wait(delayTime)
            delayed = false
        end
    end
end)
while true do
    wait(2)
    print(delayed)
end
0
I think I know what you're doing here, but I'm sure there's an easier way of going about this. Antelear 185 — 2y

2 answers

Log in to vote
1
Answered by
Antelear 185
2 years ago
local rep = game.ReplicatedStorage
local mouseDetection = rep.MouseDetection
local UIS = game:GetService("UserInputService")
local delayed = false
local delayTime = 0
UIS.InputBegan:Connect(function(input)
    if delayed == false then
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            delayed = true
            mouseDetection:FireServer()
        elseif delayed == true then
            wait(delayTime)
            delayed = false
        end
    end
end)
while true do
    wait(2)
    print(delayed)
end

Your delayed variable is used as a way of stopping a script from running when you don't want it to. For example:

local debounce = false -- basically your delayed variable

while task.wait() do -- task.wait is a more ideal way of waiting, with better performance. use it.
    if debounce == true then return end -- if db is true, then stop the script here.

    debounce = true -- if it isn't true, then skip that line and run this instead.
    print("Hello!!")
    task.wait(2)
    debounce = false -- this means we can run it again.
end

debounce is a very useful tool to make cooldowns for tools, important scripts etc. So by THAT logic, let me show you what you can change to make this work:

local MouseDetection = game.ReplicatedStorage.MouseDetection
local UIS = game:GetService("UserInputService")

local delayed = false
local delayTime = 0

UIS.InputBegan:Connect(function(Input, IsTyping)
    if delayed == true then return end

    if Input.KeyCode = Enum.KeyCode.MouseButton1 and not IsTyping then
        delayed = true
                    MouseDetection:FireServer()
    end -- IsTyping is checking if you're typing while making this input, if you're not then it will run.
end)

As you could see I used return, this is because return is returning a value, but since we return nothing and just use end, you don't need to worry about excess values being added. That line of code only run if it's true however, so if it's not true it will go further along down the lines of code and will continue ?to run the script.

One more thing: If you don't necessarily need an elseif, then don't use it. If you're only looking for 1 outcome, and not 2 or more, then just use a single IF, and not an else/elseif. I hope this helped you understand what you're trying to do. If this is not what you requested however, then please be more specific with your question. Thank you!

(P.S I didn't add the delayTime variable anywhere else so you can figure it out for yourself :] )

0
HOLY CRAP YOU ARE A LIFE SAVER TOO thecoolguy436 38 — 2y
0
no problem ;] Antelear 185 — 2y
Ad
Log in to vote
5
Answered by
imKirda 4491 Moderation Voter Community Moderator
2 years ago

You possibly wanted to implement something like this:

local rep = game.ReplicatedStorage
local mouseDetection = rep.MouseDetection
local UIS = game:GetService("UserInputService")
local delayed = false
local delayTime = 0

UIS.InputBegan:Connect(function(input)
    -- If "delayed" is not nil and not false then continue, otherwise
    -- return from the function (stop the function, it won't continue)
    if delayed then
        return
    end

    -- since it did continue here, you can assume that "return" was
    -- not used and that "delayed" is true

    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        delayed = true
        mouseDetection:FireServer()
        task.wait(delayTime)
        delayed = false
    end
end)

while true do
    task.wait(2)
    print(delayed)
end

Your code was checking if delayed is false, if yes then it would check if user pressed left mouse button, however, if user did not press it, your script would check if delayed is true, however, since previously you checked if it's false, this code would only run if it's false, thus this if statement would never run. Notice I used task.wait, it's a newer version of wait that has better performance, prefer using it.

0
THANK YOU YOUR A LIFE SAVER thecoolguy436 38 — 2y

Answer this question