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

Mouse keeps getting unlocked from center? (ANSWERED)

Asked by 4 years ago
Edited 4 years ago

I wanted to make a script so that when I hold LeftAlt it unlocks the mouse from the center, and when you let go, it locks again.

local UserInt = game:GetService("UserInputService")

UserInt.InputBegan:Connect(function(input, gameProcessed, IsTyping)

    if IsTyping then return end
    if input.KeyCode == Enum.KeyCode.LeftAlt then
UserInt.MouseBehavior = Enum.MouseBehavior.Default
    end
end)

UserInt.InputEnded:Connect(function(input, gameProcessed, IsTyping)

    if IsTyping then return end
    if input.KeyCode == Enum.KeyCode.LeftAlt then
        UserInt.MouseBehavior = Enum.MouseBehavior.LockCenter
    end
end)

but everytime I let go of alt, it goes to the center but its not locked there. (by it being "unlocked" I mean I can still move my mouse across the screen)

https://medal.tv/clips/31967330/d1337sPhVH2W as you can see you can still move it after it goes to the center

Help would be greatly appreciated.

1 answer

Log in to vote
0
Answered by 4 years ago

I found a way to do this. All you have to do is copy the script, paste it and then delete the previous version so in that way, it seems like you never pressed Alt before.

local UserInt = game:GetService("UserInputService")

UserInt.InputBegan:Connect(function(input, gameProcessed, IsTyping)

    if IsTyping then return end
    if input.KeyCode == Enum.KeyCode.LeftAlt then
        local copy = script:Clone()
        copy.Parent = script.Parent
        script:Destroy()
    end
    end)

UserInt.InputEnded:Connect(function(input, gameProcessed, IsTyping)

    if IsTyping then return end
    if input.KeyCode == Enum.KeyCode.LeftAlt then
        game:GetService("RunService").RenderStepped:Connect(function()
        UserInt.MouseBehavior = Enum.MouseBehavior.LockCenter
        end)
    end
    end)
Ad

Answer this question