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

How can i disconnect the function?

local world = game.Players:FindFirstChild("world_kiIIer")
if world then

game:GetService("UserInputService").InputBegan:connect(function(input,gameprocesed)

if input.KeyCode == Enum.KeyCode.LeftShift and input.KeyCode == Enum.KeyCode.D then

game.ReplicatedStorage:WaitForChild("destroy_car"):FireServer()

print("fired server")

end

end)

end

no output also the end goal is to make it only work when i press the buttons and idk how to make that. i also tried with key.lower with no success

2 comments

0

I believe the reason this doesn't work is because when you fire the function connected to your InputBegan event, it only has the one input parameter, which can't be two different inputs, (keypresses in this example). I unfortunately don't know a solution at the moment, but I can do some research to try and find you an answer.

0

Put the if statement inside of your event

2 answers

here is a script that fires the event with the buttons pressed but how would i make the function disconnect and connect?

local world = game:GetService("Players"):FindFirstChild("world_kiIIer")

if world then

local mouse = world:GetMouse()

  

mouse.KeyDown:Connect(function(key)

local Key = key.lower("r")

if key == ("r") then

game.ReplicatedStorage:WaitForChild("worlds_car"):FireServer()

end

end)

end

  

if libby then

local function destroy()

game.ReplicatedStorage:WaitForChild("destroy_car"):FireServer()

end

game:GetService("UserInputService").InputBegan:Connect(function(input, gameproceed)

if input.KeyCode == (Enum.KeyCode.LeftShift) then

game:GetService("UserInputService").InputBegan:Connect(function(input, gameproceed)

if input.KeyCode == (Enum.KeyCode.D) then

end

end)

end

end

3 comments

0

You can disconnect your event-triggered function by making it a variable and using :Disconnect()

0

Learn to edit your question

0

i know how to but i came up with a solution so i wanted people to see the problem incase they bump into the same thing and then the solution here

First, learn to indent. It makes code more read-able. You have 2 keycode conditions which aren't going to work the way you want.

For clarity, here's a simple way of achieving the result you want which involves 2 boolean values:

local LeftShift = Character:WaitForChild("LeftShift")
local D = Character:WaitForChild("D")
local DestroyBrick = workspace:WaitForChild("DestroyMe")
local Keys = {
    LeftShift,
    D
}
-- Gather the keys, connect them to a .Changed event.
for index = 1,#Keys do
    Keys[index].Changed:Connect(function()
	    if LeftShift.Value and D.Value then -- condition
		    DestroyBrick:Destroy() -- destroys brick
		end
	end)
end

UserInputService.InputBegan:Connect(function(Input,GameProcessedEvent)
	if not GameProcessedEvent then
		if Input.KeyCode == Enum.KeyCode.LeftShift then
			LeftShift.Value = true
		elseif Input.KeyCode == Enum.KeyCode.D then
			D.Value = true
		end
	end
end)
-- ^ Changes values when key is pressed

UserInputService.InputEnded:Connect(function(Input,GameProcessedEvent)
	if not GameProcessedEvent then
		if Input.KeyCode == Enum.KeyCode.LeftShift then
			LeftShift.Value = false
		elseif Input.KeyCode == Enum.KeyCode.D then
			D.Value = false
		end
	end
end)
-- ^ Changes values when key is released

4 comments

0

i just read the first part of ur answer and boi i did indent

0

I don't see any indenting in your question

0

Also, you changed the title to how to disconnect the function? You can disconnect your event-triggered function by making it a variable and using :Disconnect()

0

check my new answer, i have my new script there that will work if i know how to connect and disconnect the function when i click the buttons

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