How would I make a Hat giver using an ID given by the user?

I’m trying to make a hat giver that will give you the hat of the id you have entered for example, you enter in the id of a fedora, you get the fedora. How would I make that as an UI button?

2 Likes

I’m wondering as I’m going to add it for my donator perk UI: image

You could use InsertService:LoadAssest(ID) to load the asset

I tried that, it’s the same thing used for the face, but it’s really confusing for me .:confused:

Well, the same way you did it for face.

I’ve tried, doesn’t seem to work.

I’m on mobile, sorry if I mess a few things up.

local Input = --Reference the input box.
local hatID = Input.Text

for i,v in pairs(game:GetService("InsertService"):LoadAsset(hatID):GetChildren()) do
    v.Parent = player.Backpack --Reference the player beforehand.
end

The reason I used :GetChildren() is because the asset gets loaded in as a model, so we would need to get the accessory which is a descendant of the model.

It should be noted you should use MarketPlaceService to check the asset type of something you’re going to be potentially inserting because if a user inputs for example a model they uploaded with a script they could potentially run code on your game.

Perhaps she could use if v:IsA("Accessory")? Or I may be wrong.

I attempted to use this and it came with this error: image I don’t think the text is being picked up after apply has been clicked.

Current script:

script.Parent.Parent.hatApply.MouseButton1Click:Connect(function(player)
local Input = script.Parent
local hatID = Input.Text

for i,v in pairs(game:GetService("InsertService"):LoadAsset(tonumber(hatID)):GetChildren()) do
    v.Parent = player.Backpack 
	end
end)

MY BAD!! I have no idea why I said to add it to the Backpack, it should be the Character.

So something like:

script.Parent.Parent.hatApply.MouseButton1Click:Connect(function(player)
local Input = script.Parent
local hatID = Input.Text

for i,v in pairs(game:GetService("InsertService"):LoadAsset(tonumber(hatID)):GetChildren()) do
    if v:IsA("Accessory") then
        v.Parent = player.Character
    end
end

On mobile again sorry if I messed up stuff.

I have corrected that in the script now, but argument 1 is still missing or nil.

I noticed it’s being run from a LocalScript? You should use a RemoteEvent so that the hat is visible to all players and not just the client executing the script.

It’s being ran by a script. image

Oh, I don’t think MouseButton1Click in PlayerGuis can be run by ServerScripts. I may be wrong, but my current donor/admin panel utilises RemoteEvents; so using LocalScripts only and ServerScripts to carry out the function.

Okay, I’ll try edit it a bit. (30charrss)

I’m lost within seconds lol, is there any way to fit it into this script that handles all the functions?

-- Services --

local InsertService = game:GetService("InsertService")
local RepStorage = game:GetService("ReplicatedStorage")

-- Variables --

local ApplyFace = RepStorage:WaitForChild("ApplyFace")
local RemoveHead = RepStorage:WaitForChild("RemoveHead")
local AddHead = RepStorage:WaitForChild("AddHead")
local ApplyHat = RepStorage:WaitForChild("ApplyHat")

-- Functions --

function ApplyFace_Event(player, id)
	local asset = InsertService:LoadAsset(tonumber(id))
	local faceid = asset:WaitForChild("face").Texture
	
	if player.Character then
		player.Character.Head.face.Texture = faceid
		return "Applied"
	end
	
	asset:Destroy()
	return "Could not apply face"
end

function RemoveHead_Event(player, id)
	
	if player.Character then
		player.Character.Head.Transparency = 1
		player.Character.Head.face.Transparency = 1
		return "Applied"
	end
	
	return "Could not remove head"
end

function AddHead_Event(player, id)
	
	if player.Character then
		player.Character.Head.Transparency = 0
		player.Character.Head.face.Transparency = 0
		return "Applied"
	end
	
	return "Could not add head"
end


-- Callbacks --

ApplyFace.OnServerInvoke = ApplyFace_Event
RemoveHead.OnServerInvoke = RemoveHead_Event
AddHead.OnServerInvoke = AddHead_Event

Ok, let’s do it like this.

First things first, let’s create a RemoteEvent under our TextButton and name it “RequestAsset”.

Now let’s create a LocalScript that will fire this RemoteEvent whenever the TextButton is clicked (again, under the TextButton).

Ever since workspace.FilteringEnabled was enforced, any text the client enters into a TextBox was NOT replicated to the server. We want to send the text AND the player in question to the server so that the server knows what assetId to load and what player to load it on.
Something like this;

local RemoteEvent = script.Parent:FindFirstChildWhichIsA("RemoteEvent")
local AssetId = script.Parent.Parent:FindFirstChildWhichIsA("TextBox")

script.Parent.MouseButton1Click:Connect(function()
    RemoteEvent:FireServer(AssetId.Text)
end)

I want you to note that I’m assuming you have the TextBox to enter the assetId into and the TextButton we want to press to get the hat is under the same ancestor.

Now let’s create our Script under the same TextButton.

First we’ll want to get the RemoteEvent the player fires;

local InsertService = game:GetService("InsertService") --We'll need this.
local RemoteEvent = script.Parent:FindFirstChildWhichIsA("RemoteEvent")

Alright, now we’ll start listening for any events;

RemoteEvent.OnServerEvent:Connect(function(player, assetId)
    local Model = InsertService:LoadAsset(assetId) --We loaded in the asset
    
    if not player.Character then
        player.CharacterAdded:Wait() --Just in case the player does not have a character yet
    end
    for h, g in pairs(Model:GetDescendants()) do
        if g:IsA("Accessory") then
            g.Parent = player.Character
        end
    end
    Model:Destroy() --Remember to destroy the Model afterwards!
end)

If you did everything correctly, it should look like this: Baseplate.rbxl (23.7 KB)

6 Likes