Script not working with variable despite working when manually putting in number

Hello. I have been trying to make a script clear your accessories and then load in your current roblox avatar accessories.

local player = game.Players:FindFirstChild(username)
local playeruserid = player.UserId
print(playeruserid)
print(type(playeruserid))
				
clearAccessories(character) -- This is working like it should. Don't mind the "character" variable.
wait(0.5)			
character.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(playeruserid))

The issue is the last line of the code not running correctly. If I replace the “playeruserid” variable with a userid manually, such as “1081709372” the script works fine. I can’t figure out why, because using the two prints I can see that I get a correct userid in the variable and it is of the number type.

It should be the same as if I put it in manually, considering it’s an ID in the number type?
I did try running the tonumber function prior to putting it in without success.

It just realized it is working with other userIDs but it isn’t working with my own. Very strange.

Found this out by manually putting my own ID into the script. Any other ID I have tried works just fine.

Hello! I think I have found a solution to your problem. Here it is:

    local Players = game:GetService("Players")
    Players.PlayerAdded:Connect(function(player)

    local playerID = player.UserId
    print(playerID)

    -- Continue your script further on

end)

You can use any event you would like, just make sure to include the “player” in the parameters

Hello! Thanks for your help.

I tried this with no success. I’m pretty sure the issue lies somewhere in the last line of code in the original post, seen as I get a correct userid in the print(playeruserid) line.

Here’s the entirety of the code if it helps in any way. It’s quite large. Look for the row with
“if part:FindFirstChild(“TemporaryGiver”) then”

That’s where all of this applying stuff is happening. The code is quite messy since I’m actively working on it, my apologies.

-- Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
--local Create = require(ReplicatedStorage.Modules.Create)

-- Objects
local GiveArmor = ReplicatedStorage:WaitForChild("GiveArmor")
local Armor = ServerStorage:WaitForChild("Armor")

--print("TimUseless ARMOURGIVERSERVER ONLINE ///////////////////////////////////")
local seriousFaces = {
	"rbxassetid://14030506",
	"rbxassetid://168332209",
	"rbxassetid://277939506",
	"rbxassetid://255828374", 
	"rbxassetid://209715003",
	"rbxassetid://14030506",
	"rbxassetid://398670843", -- New
	}

local clearAccessories = function(character)
	--for _, child in pairs(character:GetChildren()) do
	--	if child:IsA("Accessory") then
	----		child:Destroy()
	--	end
	--end
	
	local childs = character:GetChildren()
	for i,v in pairs(childs) do
		if v:isA("Accessory") then
			v:Destroy()
		end
	end
end

local function applyDescription(character, userId)
	print("Applying", userId, "description to", character)
	local humanoid = character:FindFirstChildWhichIsA("Humanoid")
	local description = nil
	local success, message = pcall(function()
		description = Players:GetHumanoidDescriptionFromUserId(userId)
	end)
	if success then
		humanoid:ApplyDescription(description)
	else
		warn(userId, message)
	end    
end

local removeFace = function(character)
	if character and character:FindFirstChild("Head") then
		for _, child in pairs(character.Head:GetChildren()) do
			if child.Name == "face" or child.Name == "Face" then
				child:Destroy()
			end
		end
	end
end

local giveFace = function(character)
	removeFace(character)
	wait()
	local head = character:FindFirstChild("Head")
	if head then
		local face = Instance.new("Decal")
		face.Name = "face"
		face.Texture = seriousFaces[math.random(1, #seriousFaces)]
		face.Parent = head
	end	
end

--print("TimUseless ARMOURGIVERSERVER LOAD ///////////////////////////////////")

for i, v in ipairs(game:GetService("Workspace").ArmorGivers:GetChildren()) do
	local prompt = Instance.new("ProximityPrompt")
	prompt.HoldDuration = 0.5
	prompt.MaxActivationDistance = 5
	prompt.ObjectText = v.Name
	prompt.ActionText = "Grab your equipment."
	prompt.Parent = v
	prompt.Triggered:Connect(function(player)
		local part = prompt.Parent
		if part:FindFirstChild("GroupID") then 
			if not player:IsInGroup(part.GroupID.Value) then
				return
			end
		end
		local armorSet = Armor:WaitForChild(part.Name)
		local character = player.Character
		if not character then return end
		if character:FindFirstChild("ArmorEquipped") then
			print("You already have a set of armour on! Giving you new one anyways...")
			print("Resetting stats")
			character.ArmorEquipped:Destroy()
			if part.Name == "MedArmband" then
				print("It's the medic armband! No stat reset! Give medkit!")
				local ToolClone = ServerStorage.Tools:FindFirstChild("Medkit"):Clone()
				ToolClone.Parent = player:FindFirstChildOfClass("Backpack")
			else
				character.Humanoid.MaxHealth = 100
				character.Humanoid.Health = 100
				--character.LatexValues.FullImmunity.Value = false
				--local character = player.Character or player.CharacterAdded:Wait()
				--local humanoid2: Humanoid? = character:WaitForChild("Humanoid", 10)

				--humanoid2.MorphAssigned.Value = 17
			end
			--return
		end
		local armor = Instance.new("Folder")
		armor.Name = "ArmorEquipped"
		armor.Parent = character

		local sFace = armorSet:FindFirstChild("SeriousFace")
		if sFace and sFace.Value then
			giveFace(character)
		end

		local cAccessories = armorSet:FindFirstChild("ClearAccessories")
		if cAccessories and cAccessories.Value then
			clearAccessories(character)
		end

		task.wait()
	
		if part:FindFirstChild("SpeedBoost") then
			--character.Humanoid.WalkSpeed += part.SpeedBoost.Value
			local character = player.Character or player.CharacterAdded:Wait()
			local humanoid2: Humanoid? = character:WaitForChild("Humanoid", 10)
			
			--humanoid2.MorphAssigned.Value = part.SpeedBoost.Value
			--Create("NumberValue", humanoid2)({
			--	Value = tonumber(part.SpeedBoost.Value),
			--	Name = "MorphAssigned"	
			--})
		end
		
		if part:FindFirstChild("TemporaryGiver") then
			if character:FindFirstChild("TemporaryEquip") then
				character.TemporaryEquip:Destroy()
				print("You are already wearing the temp outfit")
				
				local username = character.Name
				--local Children = game.ServerStorage.Armor.Usersets[username]:GetChildren()
				print ("CLEARING ACCESSORIES")
				clearAccessories(character)
				task.wait()
				local userset = game.ServerStorage.Armor.Usersets[username]
				character.Humanoid:ApplyDescription(userset.HumanoidDescription)
				--[[for i = 1, #Children do
					if Children[i]:IsA("Accessory") then
						--print("WE FOUND AN ACCESSORY")
						print(Children[i].Name)
						Children[i]:Clone().Parent = character
					end
				end--]]
				character.Shirt.ShirtTemplate = game.ServerStorage.Armor.Usersets[username].Shirt.ShirtTemplate
				character.Pants.PantsTemplate = game.ServerStorage.Armor.Usersets[username].Pants.PantsTemplate
				game.ServerStorage.Armor.Usersets[username]:Destroy()
			else
				local temporaryequipped = Instance.new("Folder")
				temporaryequipped.Name = "TemporaryEquip"
				temporaryequipped.Parent = character
				
				local username = character.Name
				print(username)
				local folder = Instance.new("Folder", game.ServerStorage.Armor.Usersets)
				folder.Name = username
				
				--local player = game.Players:FindFirstChild(username)
				local userid = player.UserId
				print(userid)
				--print(type(playeruserid))
				
				
				local Children = character:GetChildren()
	
				for i = 1, #Children do
					--if Children[i]:IsA("Accessory") then
						--print("WE FOUND AN ACCESSORY")
					--	print(Children[i].Name)
					--	Children[i]:Clone().Parent = folder
					--end
					if Children[i]:IsA("Shirt") then
						local shirt = Children[i]:Clone()
						shirt.Name = "Shirt"
						shirt.Parent = folder
					end	
					if Children[i]:IsA("Pants") then
						local pants = Children[i]:Clone()
						pants.Name = "Pants"
						pants.Parent = folder					
					end	
					-- End of tempgiver
				end
				
				
				local userset = game.Workspace[username].Humanoid.HumanoidDescription
				userset.Parent = game.ServerStorage.Armor.Usersets[username]
				
				clearAccessories(character)
				task.wait()
				--if not game.ServerStorage.Armor.Originalusers:FindFirstChild(username, true) then
					--character.Humanoid:ApplyDescription(game.ServerStorage.Armor.Originalusers[username])
				--else
					--local characterdescription = game.Players:GetHumanoidDescriptionFromUserId(2312416938)
					--characterdescription.Parent = game.ServerStorage.Armor.Originalusers
				--end
				--local firstpart = game.Players:GetHumanoidDescriptionFromUserId(135899398)
				--character.Humanoid:ApplyDescription(game.Players:GetHumanoidDescriptionFromUserId(37092313))
				applyDescription(character, userid)
				wait(1)
				
				character.Shirt.ShirtTemplate = game.ServerStorage.Armor.GOWNGIVER.Shirt.ShirtTemplate
				character.Pants.PantsTemplate = game.ServerStorage.Armor.GOWNGIVER.Pants.PantsTemplate
				
				
				-- APPLY NEW CLOTHING
				--[[for _, child in pairs(armorSet:GetChildren()) do	
					--if child:IsA("Accessory") then
					--	local existingAccessory = character:FindFirstChild(child.Name)
					--	if existingAccessory then
					--		existingAccessory:Destroy()
					--	end
					--	local accessory = child:Clone()
					--	character:WaitForChild("Humanoid"):AddAccessory(accessory)
					--end	
					if child:IsA("Shirt") then
						if character:FindFirstChild("Shirt") then
							character.Shirt:Destroy()
						end
						task.wait()
						local shirt = child:Clone()
						shirt.Name = "Shirt"
						shirt.Parent = character
					end	
					if child:IsA("Pants") then
						if character:FindFirstChild("Pants") then
							character.Pants:Destroy()
						end	
						task.wait()
						local pants = child:Clone()
						pants.Name = "Pants"
						pants.Parent = character					
					end		
				end ]]		
			end
		else-- some more code that isn't relevant 

I found the culprit. Roblox doesn’t run the applydescription function due to thinking I already have the description on. I solved the issue by applying another description then applying my own. Roblox caching being wonky.

Refer to this: Humanoid:ApplyDescription() not working as intended

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.