What i’m trying to do is create a gui, that fires a remote event that changes the player’s character (like a morph) I want to make it so the player’s character is permanent (until they change the character from another button) How would i do this?
Hey,
You can save the selected morph name In a variable/value. And when the player respawns, just change them to the selected morph.
And when they select another morph, change that variable/value.
local function changeCharacter(player)
local search = game:GetService("ReplicatedStorage"):FindFirstChild(player.CharacterValue.Value):Clone() --Find the custom character
if search then --If the custom character exists
search.Parent = workspace
player.Character:Destroy()
player.Character = search --Change it
end
end
game:GetService("Players").PlayerAdded:Connect(function(player) --When played rjoins
local CharacterValue = Instance.new("StringValue")
CharacterValue.Value = "None"
CharacterValue.Name = "CharacterValue" --Create a value in it
CharacterValue.Parent = player
player.CharacterAdded:Connect(function(char) --When the player spawns/respawns
changeCharacter(player) --change it back.
end)
end)
That should work, do you change the CharacterValue when you equip a different one?
Also, do you want the character to also be loaded after the player leaves and rejoins?
It is because the character will be added, so then it destroys the character and makes a new one. Then that new one fires the character added event, and makes a new one. So this loop repeats forever.
I believe it’s causing that error because when the character is added, it runs that function that destroy sthe current character and sets it to a new one, which causes the CharacterAdded event to run again, causing a loop that’ll never end, causing the error to happen.
I believe a fix for this would be to make check if player.Character is equal to the searched character or not
local function changeCharacter(player)
local search = game:GetService("ReplicatedStorage"):FindFirstChild(player.CharacterValue.Value):Clone() --Find the custom character
if search and player.Character ~= search then --If the custom character exists and character is not custom character
search.Parent = workspace
player.Character:Destroy()
player.Character = search --Change it
end
end
game:GetService("Players").PlayerAdded:Connect(function(player) --When played rjoins
local CharacterValue = Instance.new("StringValue")
CharacterValue.Value = "None"
CharacterValue.Name = "CharacterValue" --Create a value in it
CharacterValue.Parent = player
player.CharacterAdded:Connect(function(char) --When the player spawns/respawns
changeCharacter(player) --change it back.
end)
end)
I’m going off of pure speculation so I’m not sure if this what has to be done
A fix for the camera would be to set the camerasubject to the player, which in this case would probably just need to be
local function changeCharacter(player)
local search = game:GetService("ReplicatedStorage"):FindFirstChild(player.CharacterValue.Value):Clone() --Find the custom character
if search and player.Character ~= search then --If the custom character exists and character is not custom character
search.Parent = workspace
player.Character:Destroy()
player.Character = search --Change it
workspace.CurrentCamera.CameraSubject = player.Character:FindFirstChildOfClass("Humanoid") --Changed to FindFirstChildOfClass incase your humanoid is named differently
end
end
game:GetService("Players").PlayerAdded:Connect(function(player) --When played rjoins
local CharacterValue = Instance.new("StringValue")
CharacterValue.Value = "None"
CharacterValue.Name = "CharacterValue" --Create a value in it
CharacterValue.Parent = player
player.CharacterAdded:Connect(function(char) --When the player spawns/respawns
changeCharacter(player) --change it back.
end)
end)
If your character has a humanoid in it, which most definitely does, once it finds it, the currentcamera’s subject becomes the new character’s humanoid through this if I’m not mistaken