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

onPlayer Added morph?

First this is my reference where i think it will help me , i added these two on my script.. but it not working.. What i want to do is when a player enters game the Morph1 childrens Arm1 and other body parts will be copy to him.. the morph1 is located in lohghning..

http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild http://wiki.roblox.com/index.php?title=PlayerAdded

I really tried hard editing this.. this script was ontouch function but i edited it.. no work..

local Players = game:GetService("Players")
function onPlayerAdded(player)
	function findFirstChild(instance, name, recursive)
if hit:findFirstChild("Humanoid") ~= nil and hit:findFirstChild("Arm1") == nil then
		local g = game.Lighting.Morph1.Arm1:clone()
		g.Parent = hit
		local C = g:GetChildren()
		for i=1, #C do
			if C[i].className == "Part" then
				local W = Instance.new("Weld")
				W.Part0 = g.Middle
				W.Part1 = C[i]
				local CJ = CFrame.new(g.Middle.Position)
				local C0 = g.Middle.CFrame:inverse()*CJ
				local C1 = C[i].CFrame:inverse()*CJ
				W.C0 = C0
				W.C1 = C1
				W.Parent = g.Middle
			end
				local Y = Instance.new("Weld")
				Y.Part0 = hit["Left Arm"]
				Y.Part1 = g.Middle
				Y.C0 = CFrame.new(0, 0, 0)
				Y.Parent = Y.Part0
		end

		local h = g:GetChildren()
		for i = 1, # h do
			if h[i].className == "Part" then
				h[i].Anchored = false
				h[i].CanCollide = false
			end
		end
		
	end

end

Players.PlayerAdded:connect(onPlayerAdded)

1 answer

✓ Accepted answer

The code you posted has a syntax error, because there's a random function definition on the third line. Get rid of that to begin with.


hit is never defined -- before, it was from the part contacting the button.

You want to get the player's Character if you're doing things like looking for a Humanoid.

You could just use player.Character, but that will only work the first time they spawn. You want to use the .CharacterAdded event.

In any case, it's easier to make a function which dresses up a given character:

function morph(character)
	local g = game.Lighting.Morph1.Arm1:clone()
	g.Parent = character
	local C = g:GetChildren()
	for i=1, #C do
		if C[i].className == "Part" then
			local W = Instance.new("Weld")
			W.Part0 = g.Middle
			W.Part1 = C[i]
			local CJ = CFrame.new(g.Middle.Position)
			local C0 = g.Middle.CFrame:inverse()*CJ
			local C1 = C[i].CFrame:inverse()*CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = g.Middle
		end
		local Y = Instance.new("Weld")
		Y.Part0 = character["Left Arm"]
		Y.Part1 = g.Middle
		Y.C0 = CFrame.new(0, 0, 0)
		Y.Parent = Y.Part0
	end

	local h = g:GetChildren()
	for i = 1, #h do
		if h[i].className == "Part" then
			h[i].Anchored = false
			h[i].CanCollide = false
		end
	end
end

Now we can just make morph happen whenever a character spawns using the CharacterAdded event. To be safe, we might want to call it on their original character, too, in case we weren't quick enough.

function onPlayerAdded(player)
	player.CharacterAdded:connect( morph )
	if player.Character then
		morph(player.Character)
	end
end
game:GetService("Players").PlayerAdded:connect( onPlayerAdded )

You can simplify your morph function a little. For instance, C and h are the same thing. You should use :IsA("BasePart") rather than .className == "Part". You should also use good variable names! What about g describes that it's an arm part being added to the player? What about h suggests it's pieces of the arm?

You also add new weld to the left arm for each part in the arm, but it's only attached to one. Either that should be using, e.g., C[i] instead of g.Middle, or you should just do it after (or before) the loop.

You can make slightly more readable code using generic for loops, too, by avoiding all of the [i]:

function morph(character)
	local newArm = game.Lighting.Morph1.Arm1:clone()
	newArm.Parent = character
	for _, child in pairs(newArm:GetChildren()) do
		if child:IsA("BasePart") then
			local W = Instance.new("Weld")
			W.Part0 = newArm.Middle
			W.Part1 = child
			local CJ = CFrame.new(newArm.Middle.Position)
			local C0 = newArm.Middle.CFrame:inverse()*CJ
			local C1 = child.CFrame:inverse() * CJ
			W.C0 = C0
			W.C1 = C1
			W.Parent = newArm.Middle
			--
			child.Anchored = false
			child.CanCollide = false
		end
	end
	local Y = Instance.new("Weld")
	Y.Part0 = character["Left Arm"]
	Y.Part1 = newArm.Middle
	Y.C0 = CFrame.new(0, 0, 0)
	Y.Parent = Y.Part0
end

1 comment

0

THNXS TO U I LEARN ALOT FROM THIS THNXXX

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