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

How do I concatenate when everything is made in a script?

Asked by
LawlR 182
6 years ago
hatsfolder = Instance.new("Folder",game.lighting)
hatsfolder.Name = "Hats Storage"

function RemoveHats(hit)
    if hit.Parent:findFirstChild("Humanoid") then
        local char = hit.Parent
        local name = (char.Name .. "'s Hats")
        local lighting = (game.Lighting["Hats Storage"])
        local parent = (lighting..game)
        for i,v in pairs(game.Lighting:GetChildren()) do
            if v.Name == "Hats Storage" then
                for i,c in pairs(v:GetChildren()) do
                    if c.Name == (name) then
                    local HatsModel = Instance.new("Model",hatsfolder)
                    HatsModel.Name = (hit.Parent.Name .. "'s Hats")
                    end
                end
            end
        end
        for i,v in pairs(char:GetChildren()) do
            if v.ClassName == "Accessory" then
                v.Parent = parent
            end
        end
    end
end

part.Touched:Connect(RemoveHats)

I get an error saying "attempt to concatenate field 'Hats Storage' (a userdata value)" "part" is made earlier in the script.

What I am trying to do is make a folder in lighting, then when someone touches a part, their hats get removed and a model gets made in the folder called "(their name + 's hats)". The hats should move to the model but the model isn't made, only the folder.

1 answer

Log in to vote
0
Answered by
Avigant 2374 Moderation Voter Community Moderator
6 years ago

Instance.new() with two arguments is deprecated. Instead, instantiate the object, set is properties, and parent it after.

Instead of using game.Lighting for storage, you should use game.ServerStorage if the client does not need to have the instance replicated to it, or game.ReplicatedStorage if it does.

This is the line causing your error: local parent = (lighting..game)

game.Lighting and game are userdata values, and do not have concatenation defined on them. You can use tostring() to attempt to convert a userdata to a string.

You should check if hit.Parent is nil before anything else.

You should prefer Instance:FindFirstChild() with a capital F.

Typically I'd recommend checking for game.Players:GetPlayerFromCharacter(hit.Parent) after checking for hit.Parent, rather than seeing is there's a humanoid.

0
Thanks but I did it a different way and it works now. But why should I use FindFirstChild instead of findFirstChild? They both work. LawlR 182 — 6y
0
Like :connect , it is all deprecated.Deprecated things will not work KawaiiX_Jimin 54 — 6y
Ad

Answer this question