Chat Module Help

I’m making a chat module, but for some reason the morph command is telling me it is unable to cast Instance to int64, which seems weird because I turned it into a number and made sure I didn’t switch arguments. I don’t know what to fix.
Code:

local commandmodule = {}
local plrService = game:GetService("Players")
commandmodule.Commands = {
	":kick",
	":kill",
	":heal",
	":damage",
	":plrMorph"
}
function commandmodule.morph(morphChar, ID)
	local humDesc = plrService:GetHumanoidDescriptionFromUserId(ID) --Errors here when I call it in chat
	humDesc.Parent = morphChar
end
function commandmodule.messaged(msg)
	local splitstring = string.split(msg, " ")
	local stringp1 = splitstring[1]
	for i, v in pairs(commandmodule.Commands) do
		local chosenCommand = commandmodule.Commands[i]
		if stringp1 == chosenCommand then
			if i == 1 then
				local plrName = splitstring[2]
				local reason = splitstring[3]
				local findPlayer = plrService:FindFirstChild(plrName)
				if findPlayer then
					findPlayer:Kick("You have been kicked. Reason: " .. reason)
				else
					print("Error: Player cannot be found")
				end
			elseif i == 2 then
				local plrName = splitstring[2]
				local target = plrService:FindFirstChild(plrName)
				if target then
					local targetChar = target.Character
					targetChar:WaitForChild("Humanoid").Health = 0
				else
					print("Error: Player cannot be found")
				end
			elseif i == 3 then
				local plrName = splitstring[2]
				local heal = splitstring[3]
				local findPlr = plrService:FindFirstChild(plrName)
				if findPlr then
					local char = findPlr.Character
					char:WaitForChild("Humanoid").Health += heal
				else
					print("Error: Player cannot be found")
				end
			elseif i == 4 then
				local plrName = splitstring[2]
				local dmg = splitstring[3]
				local lookForPlayer = plrService:FindFirstChild(plrName)
				if lookForPlayer then
					local char = lookForPlayer.Character
					char:WaitForChild("Humanoid").Health -= dmg
				else
					print("Error: Player cannot be found")
				end
			elseif i == 5 then
				local plrName = splitstring[2]
				local plrID = splitstring[3]
				local lookForPlayer = plrService:FindFirstChild(plrName)
				if lookForPlayer then
					commandmodule:morph(lookForPlayer.Character, tonumber(plrID))
					lookForPlayer.CharacterAdded:Connect(commandmodule.morph(lookForPlayer.Character, tonumber(plrID)))
					lookForPlayer:LoadCharacter()
				else
					print("Error: Player cannot be found")
				end
			end
		end
	end
end
return commandmodule

Thanks!

Can you send a screenshot of the output?

I already gave the error but okay:


(Note: I edited the code a but but only to check the type of the player ID)
Further information: it seems like the number has been converted to an instance when the morph function has been called…

use
morphChar.Humanoid:ApplyDescription(humDesc)
instead of
humDesc.Parent = morphChar

1 Like

The line that you edited was the wrong one and the error was replaced by another one: Argument 1 missing or nil.
New code:

local commandmodule = {}
local plrService = game:GetService("Players")
commandmodule.Commands = {
	":kick",
	":kill",
	":heal",
	":damage",
	":plrMorph"
}
function commandmodule.morph(morphChar, ID)
	print(typeof(ID))
	local newID = tonumber(ID)
	local humDesc = plrService:GetHumanoidDescriptionFromUserId(newID) --Argument 1 missing or nil
	morphChar.Humanoid:ApplyDescription(humDesc)
end
function commandmodule.messaged(msg)
	local splitstring = string.split(msg, " ")
	local stringp1 = splitstring[1]
	for i, v in pairs(commandmodule.Commands) do
		local chosenCommand = commandmodule.Commands[i]
		if stringp1 == chosenCommand then
			if i == 1 then
				local plrName = splitstring[2]
				local reason = splitstring[3]
				local findPlayer = plrService:FindFirstChild(plrName)
				if findPlayer then
					findPlayer:Kick("You have been kicked. Reason: " .. reason)
				else
					print("Error: Player cannot be found")
				end
			elseif i == 2 then
				local plrName = splitstring[2]
				local target = plrService:FindFirstChild(plrName)
				if target then
					local targetChar = target.Character
					targetChar:WaitForChild("Humanoid").Health = 0
				else
					print("Error: Player cannot be found")
				end
			elseif i == 3 then
				local plrName = splitstring[2]
				local heal = splitstring[3]
				local findPlr = plrService:FindFirstChild(plrName)
				if findPlr then
					local char = findPlr.Character
					char:WaitForChild("Humanoid").Health += heal
				else
					print("Error: Player cannot be found")
				end
			elseif i == 4 then
				local plrName = splitstring[2]
				local dmg = splitstring[3]
				local lookForPlayer = plrService:FindFirstChild(plrName)
				if lookForPlayer then
					local char = lookForPlayer.Character
					char:WaitForChild("Humanoid").Health -= dmg
				else
					print("Error: Player cannot be found")
				end
			elseif i == 5 then
				local plrName = splitstring[2]
				local plrID = splitstring[3]
				local numConvertedID = tonumber(plrID)
				print(typeof(numConvertedID))
				local lookForPlayer = plrService:FindFirstChild(plrName)
				if lookForPlayer then
					commandmodule:morph(lookForPlayer.Character, numConvertedID)
					lookForPlayer.CharacterAdded:Connect(commandmodule.morph(lookForPlayer.Character, numConvertedID))
					lookForPlayer:LoadCharacter()
				else
					print("Error: Player cannot be found")
				end
			end
		end
	end
end
return commandmodule

It gave new error because I used tonumber in the defining part of the function morph

1 Like

Where’s the line I have to check?

This specific line is giving the error.

Try adding a UserId manually and check if it works. If it does, then the problem is getting the UserId from the message.

1 Like

I’m going off for a while but it gave me the error attempt to index nil with ‘ApplyDescription’, I’ll try to fix it later.

Hello? I still need some help.

Have you attempted to manually set an ID like ZacharyZaxorDDD suggested?

Yes I did, and it gave me a new error (see above)

Excuse me if I’m wrong, as I haven’t done much with scripting in the past year. But it seems like the humanoid (morphChar.Humanoid), doesn’t exist, hence the “attempt to index nil with ‘ApplyDescription’”

Sorry for the late reply. If you have seen my code, I have set the morphChar to the lookForPlayer’s character.

I fixed it, I did 3 things wrong

  1. Called the function with : instead of .
  2. Used character added in a module script, which gave me an error
  3. loaded my character, instantly resetting the morph
    For anyone who does something like this, I hope you read this post.
1 Like