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)