In this tutorial, I will show you how to make a button morph and a GUI morph for skins in your game.
(NOTE: If you've read my 'Custom Character' tutorial already, you could technically count this as an extension of that tutorial)
For either method, you will need to set up your game to fit the tutorial.
First, create/insert your morphs into the game, and make sure that their primary parts are set and that their HumanoidRootPart is NOT ANCHORED!!! I am using two R6 skins from one of my games to demonstrate, but R15 should work just fine.
Secondly, create a folder in ReplicatedStorage named 'Morphs' and insert your morphs in there. That way, we can create copies of the morphs and transform the players easily.
And that's it! We can now dive in to the methods for morphing!
First, create a green 'button' using a cylinder and name it 'nameofMorph' + MorphButton. For example, for the skin 'Pingy', I would name the button 'PingyMorphButton'. You can also add an anchored version of your skin next to the button to show off what skin you will transform into should you step on the button.
Next, insert a script into the morph button and name it 'MorphScript'. I will now show you the script and go through it so you can understand it.
local morphButton = script.Parent
local morphs = game.ReplicatedStorage.Morphs
local morph = morphs.Pingy -- Replace 'Pingy' with the name of your morph
local debounce = false
morphButton.Touched:Connect(function(hit)
if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and debounce == false then
debounce = true
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local char = morph:Clone()
char.Name = player.Name
player.Character = char
char.Parent = workspace
morphButton.BrickColor = BrickColor.new("Really red")
wait(3)
morphButton.BrickColor = BrickColor.new("Lime green")
debounce = false
end
end)
Insert this code into your script. Now for the explanation. If you don't care or already understand it, then skip to Part 2.
local morphButton = script.Parent
local morphs = game.ReplicatedStorage.Morphs
local morph = morphs.Pingy -- Replace 'Pingy' with the name of your morph
local debounce = false
These variables give us the button, the morphs folder, and the skin that you want the button to transform the player into. The debounce will be used to make sure that the button doesn't infinitely transform you.
morphButton.Touched:Connect(function(hit)
if hit.Parent and game.Players:GetPlayerFromCharacter(hit.Parent) and debounce == false then
debounce = true
This piece of code activates whenever an object touches the MorphButton. The next line of code detects whether the touched object is related to the player by checking if there is a parent and if the parent is the character of a player. It also checks if debounce is false, and if so, it makes it true so that the button can't be touched until debounce is made false again.
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
local char = morph:Clone()
char.Name = player.Name
player.Character = char
char.Parent = workspace
This part of the code handles the morphing itself. First, it gets the player that we want to transform and stores it in a variable. Then, the script creates a clone of the morph and renames it to the name of the player. Finally, it sets the player's character as the morph and then inserts the morph into the Workspace. Then you can move around as the morph!
morphButton.BrickColor = BrickColor.new("Really red")
wait(3)
morphButton.BrickColor = BrickColor.new("Lime green")
debounce = false
This last piece of code turns the button red to indicate that you cannot step on it. After 3 seconds, the button turns green to indicate that you can step on it, and debounce is set to false so that you can actually step on it.
You can create as many of these buttons as you want, and I hope button morphing works for you! But if you don't want button morphing, then there is an alternative.
First, create a ScreenGui in StarterGui and name it 'MorphGui'. Then, insert a TextButton in MorphGui and rename it 'nameofMorph' + MorphButton. For example, for the skin 'Pingy', I would name the button 'PingyMorphButton'. You can customize the MorphGui and MorphButton all you want, just make sure you have a MorphGui and a MorphButton.
Next, create a RemoteEvent in ReplicatedStorage and name it 'GuiMorph'. This will be used to create the morph in the server rather than in the client. Otherwise, it will glitch and other people won't be able to see you morphed. Your explorer should look something like this:
Now, create a LOCAL script in the MorphButton and name it 'ClientMorph'. Here is the code:
local morphButton = script.Parent
local morphs = game.ReplicatedStorage.Morphs
local morph = morphs.Pingy -- Replace 'Pingy' with the name of your morph
local guiMorphEvent = game.ReplicatedStorage.GuiMorph
local player = game.Players.LocalPlayer
morphButton.MouseButton1Click:Connect(function()
guiMorphEvent:FireServer(morph)
end)
The code is pretty simple. It stores the button, the folder of morphs, the morph that you want the player to transform into, the RemoteEvent, and the player themselves. Then, it calls a function when the button is clicked. When the button is clicked, it fires the player and the morph to the server.
Now that the client-side code is done, we need to set up the server-side code. Create a script in ServerScriptService and name it 'ServerMorph'. The code is this:
local guiMorphEvent = game.ReplicatedStorage.GuiMorph
guiMorphEvent.OnServerEvent:Connect(function(player, morph)
local char = morph:Clone()
char.Name = player.Name
player.Character = char
char.Parent = workspace
end)
This code is also quite simple and a variation of the code from Method 1. It detects when the RemoteEvent is triggered from a player. Then, it creates a clone of the morph sent by the client, and then it sets that clone to be the character of the player. You can set this up to feature multiple buttons.
NOTE: I would not recommend using both of these methods in the same game. It's redundant and it might cause unforseen problems.
Thank you for reading my tutorial! I hoped this either helped you or improved your understanding on morphing! See ya later!