Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Object to string?

Asked by
JJ_B 250 Moderation Voter
8 years ago

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) 

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. Necrorave 560 — 8y
0
It just gives me the same error. JJ_B 250 — 8y
1
Nvm it works JJ_B 250 — 8y
0
Mark the title with a big [SOLVED] at the end please! dragonkeeper467 453 — 8y

1 answer

Log in to vote
1
Answered by
Necrorave 560 Moderation Voter
8 years ago

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

0
To add to this the tostring() method will attempt to call the __tostring function in the objects metatable User#5423 17 — 8y
Ad

Answer this question