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

How do I turn my morph into a morph gui?

Basicly how do I insert this script inside of a gui so that when I click on the gui button the torso changes into this ?

function onTouched(hit)
	if hit.Parent:findFirstChild("Humanoid") ~= nil and hit.Parent:findFirstChild("Chest") == nil then
		local g = script.Parent.Parent.Chest:clone()
		g.Parent = hit.Parent
		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.Parent.Torso
				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

script.Parent.Touched:connect(onTouched)

1 answer

✓ Accepted answer

It's a little complicated, not too complicated, but let me walk you through it.

So, to do this correctly, you're going to need to make a couple of new things, as well as edit your current script.

So, to solve this problem, we're going to use something called a Remote Event. A Remote Event basically lets a LocalScript and a Normal Script communicate. So, first you want to create a remote event. I'm going to edit it into your script, but you can also just insert one into Workspace.

local RemoteEvent = Instance.new("RemoteEvent", workspace)
RemoteEvent.Name = "ChangeMorphEvent"

I didn't include your script yet so this part is shorter. However, we're going to need to include your script for this next part.

Now we're going to have to determine when the RemoteEvent is Fired, and change the morph when that happens. For this part, I'm going to change script.Parent.Touched to RemoteEvent.OnServerEvent.

local RemoteEvent = Instance.new("RemoteEvent", workspace)
RemoteEvent.Name = "ChangeMorphEvent"

function onEvent(player)
    if not workspace:WaitForChild(player.Name):findFirstChild("Chest") then
        local g = script.Parent.Parent.Chest:clone()
        g.Parent = workspace:WaitForChild(player.Name)
        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.Parent.Torso
                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

RemoteEvent.OnServerEvent:Connect(onEvent)

Other changes I made: -Every time you referenced the character, I changed it to workspace:WaitForChild(player.Name). When using OnServerevent(), that is the best way to do it.

Now that we have our Remote Event created and your script edited to work with it, we want to insert a LocalScript into your Gui button. The Local script should have this code:

script.Parent.MouseButton1Click:Connect(function()
	workspace.ChangeMorphEvent:FireServer()
end)

This script sends a message to the other script when you click the gui button. When the other script detects that message, it gives you the morph.

In Short, here's the fix to your problem:

LocalScript in Gui Button:

script.Parent.MouseButton1Click:Connect(function()
	workspace.ChangeMorphEvent:FireServer()
end)

Change Normal script to this:

local RemoteEvent = Instance.new("RemoteEvent", workspace)
RemoteEvent.Name = "ChangeMorphEvent"

function onEvent(player)
    if not workspace:WaitForChild(player.Name):findFirstChild("Chest") then
        local g = script.Parent.Parent.Chest:clone()
        g.Parent = workspace:WaitForChild(player.Name)
        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.Parent.Torso
                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

RemoteEvent.OnServerEvent:Connect(onEvent)

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