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

How do I make a giver that gives an item based on its ID?

What I want to do is make an ID based giver. For example, suppose there is a variable "id".

local id = 11453385

The ID used is the one of the Katana.

The giver should then give a Katana when touched/clicked. If the ID is changed to something else, it should give the item which corresponds to the new ID.

Is this possible? If so, how? Thanks in advance!

1 answer

Note

Your question looks more like a request for code, which the site should not be used for. Also, accept this answer if it helped solve your problem!

Question

How do I make a giver that gives an item based on its ID?

Answer

Yes, this is completely possible.

You can use InsertService's method called :LoadAsset. This method takes 1 parameter, a number, which is the ID of the asset you're trying to load.

It will return a model containing the asset you wish to insert. To quickly get the asset inside the module you can call :GetChildren() on the model and then index for the first child via [1]

local insertService = game:GetService("InsertService")

local id = <asset id>

local assetModel = insertService:LoadAsset(id) --// get the asset
local asset = assetModel:GetChildren()[1] --// get the asset in the model

asset.Parent = workspace --// put the asset in workspace

The Catch

InsertService:LoadAsset(ID) cannot be used to load assets from the client and cannot be used to load assets that you don't own which are off-sale.

Extra Information

Clicking

Roblox has a feature called ClickDetector's which are instance's you can put inside parts which will update the mouse icon when they hover over the part and allow easy clicking functionality.

Connecting a function to a ClickDetector

You can index the MouseClick signal and call the :Connect method on it and pass your function. The first argument passed to your function will be the player who clicked the part.

Some example code that will kill a player when they the block with the clickDetector in it:

local clickDetector = <reference to my click detector>

local function kill(player)
	local character = player.Character

	character:BreakJoints()
end

clickDetector.MouseClick:Connect(kill)

Touching

All BasePart's have a signal called Touched which is invoked when a physical instance touches the part.

Connecting a function to the Touched signal.

You can :Connect a function which will have the instance which touched the original part passed as the first argument.

The example code below will output the instance which touched part:


local part = < reference to my part >

local function touched(part)
	print(part.Name, "collided with me")
end

part.Touched:Connect(touched)

The Finale

You should be able to use the information provided above and below to successfully achieve the task you're looking at. If you run into problems while attempting to write some code please do ask a question on the site, just make sure to read the "How To Post Good Questions-Answers" article I linked below; having good questions really does make it easier get an answer.

Further Reading

https://developer.roblox.com/en-us/api-reference/function/InsertService/LoadAsset

https://developer.roblox.com/en-us/api-reference/class/ClickDetector

https://developer.roblox.com/en-us/api-reference/event/BasePart/Touched

https://scriptinghelpers.org/help/how-post-good-questions-answers

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