Hello! I’m still new to coding in Lua, but I’m working on a Sonic game that will eventually support more characters that use skinned meshes.
Right now, the players can only spawn in as Sonic via the good ol’ StarterCharacter method.
What I want to accomplish is a character menu that the players can unlock characters and switch to them in, similar to how Sonic Speed Simulator has multiple morphs.
The menu GUI itself isn’t much of an issue at the moment, but how switching characters works. I’ve tried to look up similar problems and even looked at someone else’s .rbxl file on character switching, but I’ve just grown more confused. Apparently you need a module script to switch characters? You need to create a Humanoid on the new model as soon as you switch characters?
Right now I have the StarterCharacter model (renamed Sonic) in my ReplicatedStorage. Would referencing from that work?
plr.Character = game.ReplicatedStorage.Sonic
--idk what context or anything this is in but you can clean this up or wtv
script from goldenstein64
local function respawnPlayer(plr, modelName)
local model = replicatedstorage.Characters:FindFirstChild(modelName)
print("Model from server is", model)
local oldModel = plr.Character
local newModel = model:Clone()
local oldCFrame = oldModel:GetPrimaryPartCFrame()
plr.Character = newModel
newModel.Parent = workspace -- [1] this follows current character loading behavior
newModel:SetPrimaryPartCFrame(oldCFrame)
oldModel:Destroy()
end
respawnEvent.OnServerEvent:Connect(respawnPlayer)
I’m wondering if that can be triggered by clicking on a GUI button… Of course they can be changed to images later down the line for each model, but would the script be put inside the ScreenGUI the images are placed in?
It sounds like the script you posted could accommodate for multiple morphs at a time.
However, what’s confusing me is the respawnEvent. I tried making a RemoteEvent named that in the ReplicatedStorage, but it doesn’t really work. The button functions as normal, but the player doesn’t respawn as the morph.
Here’s the scripts I’m using for reference:
Client Script
-- reference the script to change characters
local button = script.Parent
local function onButtonActivated()
game.ReplicatedStorage.RemoteEvent:FireServer("Sonic")
print("Clicked!")
end
button.Activated:Connect(onButtonActivated)
Server Script
local replicatedstorage = game:GetService("ReplicatedStorage")
local function respawnPlayer(plr, modelName)
local model = replicatedstorage.Characters:FindFirstChild(modelName)
print("Model from server is", model)
local oldModel = plr.Character.Humanoid
local newModel = model:Clone()
local oldCFrame = oldModel:GetPrimaryPartCFrame()
plr.Character.Humanoid = newModel
newModel.Parent = workspace -- [1] this follows current character loading behavior
newModel:SetPrimaryPartCFrame(oldCFrame)
oldModel:Destroy()
end
game.ReplicatedStorage.respawnEvent.OnServerEvent:Connect(respawnPlayer)
I know I’m doing something wrong with the respawnEvent, but can’t figure out what.
Replaced the RemoteEvent in the client script to the respawnScript, and it works.
EDIT: Mentioned earlier that animations break, these can be fixed by putting the animations inside the character.
However, the momentum I previously had on the StarterCharacter is broken. It used to be in the StarterCharacterScripts, but it doesn’t work. I’ve tried putting it in the StarterPlayerScripts, still doesn’t work.
EDIT 2: Had the character inside the momentum script set to the script’s parent (the model) and it somehow works. When using this method, be sure to check your other scripts for errors. Otherwise it’ll break just like the momentum script did.