How do I get rid of this lacking permission 5 error? Trying to press key

How do I get rid of this lacking permission 5 error? What is permission 5? I’m trying to press a key when the character finishes tweening, instead its returning this error.

LocalScript in StarterGui:

local VirtualInputManager = game:GetService("VirtualInputManager")
local ts = game:GetService("TweenService")

local speed = 15
function cool()
	while wait() do
		local Closest
		local descendants = game:GetService("Workspace").WildernessBlocks:GetDescendants()
		local PlayerPosition = game.Players.LocalPlayer.Character.HumanoidRootPart.Position

		for i, v in pairs(game:GetService("Workspace").WildernessBlocks:GetChildren()) do

			if Closest == nil then
				print("closest part hasn't been set")
				wait()
				Closest = v
				print("made closest part some random part")
			else
				wait()
				print("thinks random part is closest")
				if (PlayerPosition - v.Position).magnitude < (Closest.Position - PlayerPosition).magnitude then
					print("looking for actual closest part")
					wait()
					Closest = v
					print("actual closest part found")
					wait()
					local distance = (game.Players.LocalPlayer.Character.HumanoidRootPart.Position - Closest.Position).magnitude
					print("found distance between player and actual closest part")
					wait()
					local tween = ts:Create(game.Players.LocalPlayer.Character.HumanoidRootPart,TweenInfo.new(distance / speed),{["CFrame"] = Closest.CFrame})
					print("tween created")
					wait()
					tween:Play()
					print("tween started, waiting until tween done")
					tween.Completed:Wait()
					print("tween finished")
					VirtualInputManager:SendKeyEvent(true, "F", false, game)
					print("tried pressing f")
					wait()
					VirtualInputManager:SendKeyEvent(false, "F", false, game)
					print("worked")
				end
			end
		end
	end
end

spawn(cool())

You can’t use VirtualInputManager, that’s only used for internal use

1 Like

Then how does the player locally press a key?

You have to use UserInputService or ContextActionService. Roblox has an article about it:

You can’t locally simulate keypresses. Roblox’s reflection system locks certain reflected classes, functions, properties, etc. away from the scripting system with a security level. Scripts with a security level lower than that of a reflected item cannot use such reflected item. VirtualInputManager cannot be used from any user script because of its permission level of 5. This is good because simulating keypresses would be a major security vulnerability.

From what I see the UserInputService functions can only be used to detect keyboard presses, device, etc. not actually do them on behalf of the player. And ContextActionService looks like it can only bind keys to actions (when player presses key manually, something happens) and unbind default key presses, and some other stuff which won’t solve this problem. If I’m wrong please let me know.

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(Input)
     if Input.KeyCode == Enum.KeyCode.F then
    print("whatever")
end
end)

I am pretty sure that detects when the player presses F, not pressing F for them.

The closest you can get to simulating input from the player would be manually running a function connected to it

1 Like

This is correct. There is no accessible interface for developers to emulate any input from the user’s device. Input is readonly for security purposes. VirtualInputManager’s security level is too high for user scripts to access it.

So how do you access VirtualInputManager? If you can’t access it at all, what is the point of it?

You don’t. It’s for internal use only.

It is used internally by Roblox for input playback during performance unit tests. It can only be used by CoreScripts, which have a higher permission level. This service is just exposed to the lua API so performance benchmarking unit tests can be written using lua.
Read about it here: VirtualInputManager

2 Likes