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

Object to string?

I made a script that puts a player's regular clothes back on when they chat a specific phrase. However when this script is carried out I get an error: Workspace.clothes:28: bad argument #3 to 'PantsTemplate' (string expected, got Object) I understand what this means but I don't know how to convert the PantsTemplate into a string. Here is the script:

game.Workspace.ChildAdded:connect(function(char)
	if char:FindFirstChild("Humanoid") then
		local p = game.Players:FindFirstChild(char.Name)
		if p then
			if char:FindFirstChild("Shirt") then
				sh = char.Shirt.ShirtTemplate
			else
				sh = Instance.new("Shirt")
				sh.Parent = char
				sh.Name = "Shirt"
			end
			if char:FindFirstChild("Pants") then
				pa = char.Pants.PantsTemplate
			else
				pa = Instance.new("Pants")
				pa.Parent = char
				pa.Name = "Pants"
			end
		end
	end
end)

function onChatted(msg, recipient, speaker) 
local source = string.lower(speaker.Name) 
msg = string.lower(msg) 

if (msg == "/clothes") then 
speaker.Character.Pants.PantsTemplate = pa
speaker.Character.Shirt.ShirtTemplate = sh
end 

end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered) 

4 comments

0

Try using tostring() to convert something into a string, if its possible. Let me know if this works, I will make it an answer if it does.

0

It just gives me the same error.

1

Nvm it works

0

Mark the title with a big [SOLVED] at the end please!

1 answer

✓ Accepted answer

Whenever you wish to convert something into a string, try using tostring().

Example:

number = 5
words = "This is a string"

print(words..tostring(number))

This is only an example of course, because usually print() will usually recognize integers

In your case, you may need to convert couple different objects.

Go here for a little more info

1 comment

0

To add to this the tostring() method will attempt to call the __tostring function in the objects metatable

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