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

How do I make a script to pick up a tool?

Asked by 10 years ago

I have trouble with this. When something is on the floor or ground like a Gun or flashlight, it will print "Press E to pick up GunName" or "Press E to pick up Flashlight". then when you pick it up, you have it. How'd I do that?

2 answers

Log in to vote
2
Answered by
nate890 495 Moderation Voter
10 years ago

1. Put the following code into a LocalScript and insert it into StarterPack.
2. Put the tools into a model named "Tools" and cut/paste the model into Lighting.
3. Cut/paste  the contents of your tools into models and rename the models to the exact  same name as their corresponding tool.
4. Put the models (that look like they're tools) into workspace and position them.

Steps 2 - 4 make it so players can't pick up tools on the ground by touching them (so we're making fake "tools" that cannot be picked up when touched.)

local tools = {["Flashlight"] = true, ["Pistol"] = true}
local radius = 10
local removeToolUponPickUp = true -- true and the tool is removed when picked up, false and it stays

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local running = false
local found, hint

function getTools()
    local found = {}
    for _, c in pairs(game.Workspace:GetChildren()) do
        if tools[c.Name] then
            table.insert(found, c)
        end
    end
    return found
end

function walking(speed)
    running = speed > 0 and true or false
    while running and not found do
        for _, tool in pairs(getTools()) do
            local pos = tool:GetModelCFrame().p
            if player:DistanceFromCharacter(pos) < radius then
                found = true
                hint = Instance.new("Hint", player.Character)
                hint.Text = "Press E to pick up "..tool.Name
                found = tool
                while true do
                    if player:DistanceFromCharacter(pos) > radius then
                        hint:Destroy()
                        found = nil
                        break
                    end
                    wait(0.25)
                end
            end
        end
        wait(0.25)
    end
end

mouse.KeyDown:connect(function(key)
    if key:lower() == "e" and found then
        local tool = game.Lighting.Tools[found.Name]:Clone()
        tool.Parent = player.Backpack
        if removeToolUponPickUp then
            found:Destroy()
            if hint then
                hint:Destroy()
            end
        end
        found = nil
    end
end)

repeat wait() until player.Character
wait(0.5)
player.Character.Humanoid.Running:connect(walking)

If you find any problems let me know, I completed this while managing several other things so it wouldn't surprise me. I have tested it though and to my knowledge it functions properly.

Edit: if you don't understand the steps on how to function this thing you can visit this place (it is uncopylocked.)

Edit2: I added a half second wait after line 59 to ensure the code runs properly when in online mode.

1
Thank you :D It works Roboy5857 20 — 10y
0
Glad I could help :)] nate890 495 — 10y
0
See my second edit if you're experiencing problems with the code not functioning properly (or at all) in online mode. nate890 495 — 10y
0
Ok Roboy5857 20 — 10y
View all comments (2 more)
0
Ok so I tried using this script... But whenever I try to, It just says "Pistol is not a valid member of Model" in the output box. Any idea how to fix this? (Yea im pretty late on this) XxrobloxderpgamerxX -5 — 6y
0
yeah i know im late to answer that ^ but did you try to go to this place https://www.roblox.com/games/152526524/PickUpToolScripts-Place XxOPGUYxX1234567 221 — 4y
Ad
Log in to vote
0
Answered by 10 years ago

lol I asked that a few days ago...

0
Did you get a answer? Roboy5857 20 — 10y
0
nope, i think admins closed my question LokHsuLi 10 — 10y
0
So then, people can answer my question.., Roboy5857 20 — 10y
0
up :) I hope they do :D LokHsuLi 10 — 10y
View all comments (7 more)
0
but I can try guess, if playerinradius(numberhere) then print ("Press E to pick up" ...ToolName... "." ) onkeydown(e) script.parent.clone() = player.backpack.clone() --- am basiz scripter so just try this maybe... LokHsuLi 10 — 10y
0
cool :) LokHsuLi 10 — 10y
0
Let me know how it goes. nate890 495 — 10y
0
I didn't understand Lok. Roboy5857 20 — 10y
0
my friend tried it didnt work... he just renamed the 2 tools in the script and added 1 more, he even looked at it carefully... and he changed radius to 5 LokHsuLi 10 — 10y
1
If it didn't work before then it should work now. If your friend is still having troubles then I'd recommend that he take a look at the demo here: http://www.roblox.com/PickUpToolScripts-Place-place?id=152526524 good luck. nate890 495 — 10y
0
he said his problems are solved now LokHsuLi 10 — 10y

Answer this question