Skinstashery
Roblox
Home / Roblox / Studio script library / Give tool on touch
RB-004

Roblox give tool on touch script

A part that gives the player a tool from ServerStorage when they touch it.

Script type
Script
Where it goes
Inside the Part
Category
Doors and interaction

For your own game in Roblox Studio. Not for use in other people's games.

The code

-- Give tool on touch: copies a Tool from ServerStorage into the player's Backpack.

local TOOL_NAME = "Sword"    -- the name of a Tool inside ServerStorage
local COOLDOWN = 2           -- seconds before the same player can be checked again

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

local part = script.Parent
local tool = ServerStorage:WaitForChild(TOOL_NAME)
local busy = {}

local function playerHasTool(player)
	local backpack = player:FindFirstChildOfClass("Backpack")
	if backpack and backpack:FindFirstChild(TOOL_NAME) then
		return true
	end
	local character = player.Character
	if character and character:FindFirstChild(TOOL_NAME) then
		return true
	end
	return false
end

part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player or busy[player] then
		return
	end

	busy[player] = true

	if not playerHasTool(player) then
		local backpack = player:FindFirstChildOfClass("Backpack")
		if backpack then
			tool:Clone().Parent = backpack
		end
	end

	task.wait(COOLDOWN)
	busy[player] = nil
end)

How to set it up

  1. Make or insert a Tool (Explorer, plus button, Tool). Give it a Part named Handle.
  2. Drag the Tool into ServerStorage and rename it Sword (or any name you like).
  3. Insert a Part, add a Script to it, and paste the code. Set TOOL_NAME to your tool name.
  4. Play and touch the part. The tool appears in your hotbar.

Change it

More in Doors and interaction

All Roblox scripts

More Roblox tools