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

how i can make keydown to grab a tool (press E to grab tool) ?

Asked by 7 years ago
Edited 7 years ago

I want make a keydown "example press E to grab the tool" , and if the mouse are up the part this open a gui than say press (any key) to pick up

I need the script

1 answer

Log in to vote
0
Answered by 7 years ago

I think this script is something that will work, you'll have to edit the minor things like parents, locations in the hierarchy, etc, I also made a small GUI .

I'm not that smart in scripting but I know some stuff, so if anyone else reading this wants to comment anything bad I did, do tell me. I also don't know if I positioned the gui correctly.

mouse = game:GetService("Players").LocalPlayer:GetMouse() --We need the mouse

local tool = game:GetService("Workspace").Tool --Whatever part you want the player to hover over for the gui to appear.
local clickDetector = Instance.new("ClickDetector", tool) 

function PickupGui() --Function for the gui
        local PGui1 = Instance.new("ScreenGui",game:GetService("Players").LocalPlayer.PlayerGui)
        PGui1.Name = "PGui"
    local PGui2 = Instance.new("TextLabel")
    PGui2.BackgroundTransparency = 1
    PGui2.Position = UDim2.new(0.5, 0, 0.5, 0) 
    PGui2.Size = UDim2.new(0, 200, 0, 50)
    PGui2.FontSize = Enum.FontSize.Size24
    PGui2.Text = "Press 'E' to grab the tool."
    PGui2.Parent = PGui1

end

tool.ClickDetector.MouseHoverEnter:connect(function(Player) --Whenever player hovers mouse over the tool 
   PickupGui() --The gui is inserted into playergui.
end)

tool.ClickDetector.MouseHoverLeave:connect(function(Player) --When players mouse leaves the clickdetector
if game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("PGui") then --Looks for PGui
    game:GetService("Players").LocalPlayer.PlayerGui:FindFirstChild("PGui"):Destroy() --Destroys it
end
end)


mouse.KeyDown:connect(function(key) 
    if key == "e" then
    tool.Parent = game:GetService("Players").LocalPlayer.Backpack --Add or remove parents in order to ensure entire "Tool" is parented to Backpack. 
    end
end)

If you want to make sure players cannot get a bunch of the same items at once, replace the mouse.KeyDown function with this:

mouse.KeyDown:connect(function(key) 
    if key == "e" then
        if game:GetService("Players").LocalPlayer.Backpack:FindFirstChild("ToolName") then
            else
    tool.Parent = game:GetService("Players").LocalPlayer.Backpack --Add or remove parents in order to ensure entire "Tool" is parented to Backpack. 
        end
        end
end)
0
Thanks so much ByDiego1405 -3 — 7y
Ad

Answer this question