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

Is there a better way to manage the user input?

Hi, I have some problems to make a user take an item when they pressed E. Can you give me tips how i could make it more efficient/better? Im still a beginner in scripting


function HoverButton(text, b)
if b == true then
		hoverbutton.Text = text
		hoverbutton.Visible = true
	else
		hoverbutton.Visible = false
end

game:GetService("RunService").RenderStepped:Connect(function()
	uis.InputBegan:Connect(function(key, gameProcessedEvent)
		if key.KeyCode == Enum.KeyCode.E then
			if mouse.Target then
				if debounce == false then
					debounce = true
					mt = mouse.Target
				if mouse.Target.Name == "Metal" then
					mi:FireServer(mt)

					item = takeitem:InvokeServer(mt)
					
					_G.PutInBackpack(item)
				end
				
				wait(0.1)
				debounce = false
				end
			end
		end
	end)

	if mouse.Target then
		debo = true
		if mouse.Target.Name == "Metal" then
			HoverButton("[E] Take", true)
		else
			HoverButton("", false)
		end
		
	end
	end
end)

1 answer

✓ Accepted answer

Well UserInputService is the best way to handle input so you're checking for input the right way. However I don't see a point of using RenderStepped in this situation as InputBegan will fire outside of that function as well so it will be more efficient if you remove the RenderStepped. Also it's good that you are checking for mouse target inside of the InputBegan event so everything seems to be good just I don't see the point of RenderStepped here.

Just one tip, if you wanna disable this while chatting you can just do this:

uis.InputBegan:Connect(function(key, gameProcessedEvent)
	if gameProcessedEvent then return end --so it does not fire while you're chatting
 	--do stuff
end)

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