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

How do I make a script that copies another script to models with a certain name?

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.

2 answers

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.

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