UserInputService is not working

So with my code nothing gets printed to output, it is in a local script as well.

local crate = game.Workspace.Items["Supply Crate"]
local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local inventory = game.StarterGui.SupplyInv
local plrinv = game.StarterGui.Inventory
local isOpened = false
local Character = player.Character or player.CharacterAdded:Wait()


-- Build a "RaycastParams" object and cast the ray
local Range = 100
local ray = Ray.new(Character.Head.Position, Character.Head.CFrame.LookVector * Range)
local hit = game.WorkSpace.Items:FindPartOnRay(ray)


local function IsEKeyDown()
	if UIS:IsKeyDown(Enum.KeyCode.E) then
		return true
	end
end

UIS.InputBegan:Connect(function(input)
	while true do
		if IsEKeyDown() then
			print("E has been pressed!")
			local part = hit.Parent
			if hit and part == crate then  
				print("Found Crate")
				inventory.Enabled = true
				plrinv.Enabled = true
				isOpened = true        
			end
		end
	end 
end)
1 Like

image
you forgot to use this parameter.

if input.KeyCode == Enum.KeyCode.E then

you don’t need while true do it would lag your game and not needed. Replace with the above code

You can simply rewrite it to

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("E has been pressed!")
		local part = hit.Parent
		if hit and part == crate then  
			print("Found Crate")
			inventory.Enabled = true
			plrinv.Enabled = true
			isOpened = true        
		end
	end
end)

Thank you, Now i am getting this: FindPartOnRay is not a valid member of Folder “Workspace.Item”

On line: local hit = game.Workspace.Items:FindPartOnRay(ray)

What’s the problem with the script you can show me?

When the game loads i get this error FindPartOnRay is not a valid member of Folder "Workspace.Items"

On this line local hit = game.Workspace.Items:FindPartOnRay(ray)

What your script is exactly trying to do?

is hit a table of items hit by ray? or is it just one part that the ray will hit?
If you just want one part then it’s better of using workspace:RayCast()

So when you press ‘E’ and if you are looking at a specific part (the supply crate) then it will do stuff

I’m not too good with ray casting i got some of it from a yt video so I don’t really know how i can implement that into my code

you should look at the api reference
https://developer.roblox.com/en-us/api-reference/function/WorldRoot/Raycast

Instead of using FindPartOnRay you can check if the TextLabel is text is the crate name then you can purchase that crate.

FindPartOnRay() is an instance method of the workspace, it should be called on the workspace instance itself, i.e;

local hitPart, hitPosition, hitNormal, hitMaterial = workspace:FindPartOnRay(ray)