Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

The input KeyCode E does not work, there are no errors in output?

Asked by 2 years ago
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)


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)

Nothing is working with the script, the printing does even work when i press E. there is no errors in outpost also tried changing it from E to something else and still didn't work

1 answer

Log in to vote
1
Answered by 2 years ago
if input.KeyCode == Enum.KeyCode.E  then

Isnt an event. This piece of code just asks if input.KeyCode (nil) == Enum.KeyCode.E (nil) => (true)

So the code should be smthg like this :

local function IsEKeyDown()
    if UserInputService: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)
0
This still doesn't work Sonnymacko 19 — 2y
0
My script is a local script in starter player scripts if that changes anything? Sonnymacko 19 — 2y
Ad

Answer this question