Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

6 comments

1

Thank you :D It works

0

Glad I could help :)]

0

See my second edit if you're experiencing problems with the code not functioning properly (or at all) in online mode.

0

Ok

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)

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

lol I asked that a few days ago...

11 comments

0

Did you get a answer?

0

nope, i think admins closed my question

0

So then, people can answer my question..,

0

up :) I hope they do :D

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...

0

cool :)

0

Let me know how it goes.

0

I didn't understand Lok.

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

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.

0

he said his problems are solved now

This discussion is preserved as it was. New answers, comments, and votes are closed.