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

How do I get the screen to shake when the player holds mousebutton1?

Here's the script I made to make the screen shake on click, however it only works when I tap click. I want it to work when the player holds lmb:

local character = script.Parent
local plr = game.Players[character.Name]
local mouse = plr:GetMouse()
local mousedown = false
local cam = game.Workspace.CurrentCamera
local shaking = false
local radius = 0
local Decounce=true
mouse.Button1Down:Connect(function()
    if character:FindFirstChild("Humanoid") ~= nil then
        radius = 20
        radius = 150
        if (shaking) then return end
        shaking = true
        repeat
            wait()
            if radius ~= 0 then
                character.Humanoid.CameraOffset = Vector3.new(math.random(-radius,radius)/100,math.random(-radius,radius)/100,math.random(-radius,radius)/100)
                cam.CFrame= cam.CFrame * CFrame.Angles(math.random(-radius,radius)/14000,math.random(-radius,radius)/14000,math.random(-radius,radius)/14000)               
                radius = radius - 13
            end
        until radius <= 0
        shaking = false
        character.Humanoid.CameraOffset = Vector3.new(1,1,0)
        mouse.Button1Up:Connect(function()
        mousedown = false
        end)
        mouse.Button1Down:Connect(function()
            mousedown = true
            repeat
                wait(0.01)
            until not mousedown
            end)

    end
end)

Please help me fix my script!

1 comment

0

lol this is a repost since the original was buried in new questions

2 answers

Button1Down is an event, meaning it executes its code once until the event is triggered again. You will want to make a loop that goes until Button1Up is triggered.

Something like this

mouse.Button1Down:Connect(function()
	until mouse.Button1Up do
		-- your code
	end
end)

this might not work though. If it doesn't, try something like

mouse.Button1Down:Connect(function()
	until variable do
		-- your code
	end
	variable = true
end)

mouse.Button1Up:Connect(function()
	variable = false
end)

2 comments

0

Ok so I tried this, but I'm not sure about the formatting. I've never actually done anything involving holding a mousebutton, so could you copy and paste what it should look like in the end?

0

The mouse.Button1Down:Connect(Function()) is in the same place in the script, so put it in your script like that.

UserInputService is a good way to tackle this problem. You are able to connect a function to both when the input is started and ended.

Try something like this:

local US = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local playerMouse = Player:GetMouse()

local isHoldingButton = false
local INPUT_TYPE = Enum.UserInputType.MouseButton1

spawn(function()
	while wait() do
		if isHoldingButton then
			-- Rest of code.
		end
	end	
end)

local function checkInput(Input)
	local UserInputType = Input.UserInputType
	if UserInputType == INPUT_TYPE then
		-- Let's check if they are currently holding it.
		if not isHoldingButton then
			-- They are not holding it, so lets say they are.
			isHoldingButton = true
			return
		end
		-- They are no longer holding it.
		isHoldingButton = false
	end
end

US.InputBegan:Connect(checkInput)
US.InputEnded:Connect(checkInput)

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