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.
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.