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

How to equip an item when a block is touched?

I would like it if when I touched a block, it would put a tool from ServerStorage into the LocalPlayers' backpack and when the block isn't being touched, the tool would be removed.

4 comments

0

Could be done using Part:GetTouchingParts() probably, if anybody else knows better correct me if I'm wrong

0

You're not allowed to ask "how to make this" questions, but basically loop part:GetTouchingParts on stepped and if the player is touching it, add the item and if not, remove it

0

sorry, i didnt word it correctly. i mean if there was any way to, any functions to, not a script.

0

Well I don't really know what you mean but Part:GetTouchingParts() is something you put inside of a script that will return a table of everything touching the part. If you have a part that is CanCollide false then it will return nil. To get around that read this: https://devforum.roblox.com/t/simple-trick-to-make-gettouchingparts-work-with-non-cancollide-parts/177450

1 answer

✓ Accepted answer

the most efficient way to do this is by welding a large hitbox to the player's character and then using Touched and TouchEnded to detect when the player touches or stops touching the block

psst here's a little tip: StarterCharacterScripts is called "scripts", but anything you put in it will be cloned into the character when they spawn, regardless of what it actually is

this means that instead of having to clone or instantiate a part manually when the player spawns, we can just put a part in StarterCharacterScripts for our "hitbox" and weld it to the HumanoidRootPart using a script

make sure that the part has Anchored set to false, has CanCollide set to false, has Massless set to true, is at least a pretty decent size, and has a name

i made mine's size [3, 8, 3] but you can change it up a bit if you want it to be larger or smaller—just be sure that the hitbox is large enough to be taller and a bit thicker than the actual character, or else you might start running into problems

now you can parent a Script to that part with this as its contents:

local weld = Instance.new("Weld")
weld.Part0 = script.Parent.Parent:WaitForChild("HumanoidRootPart") -- we use WaitForChild here since the HumanoidRootPart might not have loaded yet
weld.Part1 = script.Parent
weld.Parent = script.Parent

the script "welds" the hitbox to the HumanoidRootPart, essentially "gluing" them together

as for the part that gives players the tools, you can simply give the player the tool when Touched fires (if they don't already have it) and remove it when TouchEnded fires (if they do have it)

local Players = game:GetService("Players")
local tool = path.to.Tool
local part = path.to.Part

part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and hit.Name == "Hitbox" then
		-- yes, you can use logical operators when assigning variables
		local playerTool = player.Backpack:FindFirstChild(tool.Name) or player.Character:FindFirstChild(tool.Name) -- we need to check in the player's backpack AND their character since tools get parented to the character when they're equipped
		if not playerTool then
			tool:Clone().Parent = player.Backpack
		end
	end
end)

part.TouchEnded:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player and hit.Name == "Hitbox" then
		local playerTool = player.Backpack:FindFirstChild(tool.Name) or player.Character:FindFirstChild(tool.Name)
		if playerTool then
			playerTool:Destroy()
		end
	end
end)

also, people might tell you that TouchEnded is """unreliable""" and """laggy""", but that's just because they don't know how to use it properly

yes, if you only check if the player's CHARACTER is touching the part, it will be unreliable, since the character's limbs are bouncing all over the place, but that's why we welded a large hitbox to the character

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