I need this for a game. Basically, I have a bunch of crates. Each one has an identical script that gives a random weapon. The problem is that whenever I make a new weapon I have to edit each script, and due to the size of the map, that is simply not practical to do. What I tried to make is a script that copies the crate script automatically, but I haven't been successful. Here's what I managed to make:
scan = function (t) for _,crate in pairs(t) do if crate:IsA("Model") and v.name == "cratemodel" and v.randomgun == nil then copy = game.Workspace.randomgun:Clone() copy.Parent = crate end end end scan(game:GetChildren())
In here I check every part in the game for the name then I check that the script isn't in the crate already. I then copy the script from a set location every time and move it inside the crate. Ideally, this will copy the "randomgun" script from Workspace to every model named "cratemodel", but that doesn't happen. The script does nothing. Can anyone help me with this? I can't figure out where I made a mistake, I'm really new to this.
Some random variables in your script that aren't declared.
function scan(Part) -- scan scan for i,v in pairs(Part:Children()) do scan(v) pcall(function() -- run this if v:IsA("Model") and v.Name == "cratemodel" and not v:findFirstChild("randomgun") then local copy = game.Workspace["randomgun"]:Clone() copy.Parent = v -- into the v :D end end) end end scan(game.Workspace) -- What you want to scan
You need to turn .name into .Name. Capitalization is important.