How would I fix it?
This is my server script to change the character.
local event = game:GetService("ReplicatedStorage"):WaitForChild("ChangeChar")
local chars = game:GetService("ReplicatedStorage"):WaitForChild("Characters")
local equip = game:GetService("ReplicatedStorage"):WaitForChild("Equip")
event.OnServerEvent:Connect(function(Player, char)
for _, Character in pairs(chars:GetChildren()) do
if Character.Name == char then
local newone = Character:Clone()
newone.Parent = workspace
Player.Character = newone
local humanoiddescrip = Character:WaitForChild("Humanoid"):GetAppliedDescription()
Player:LoadCharacterWithHumanoidDescription(humanoiddescrip)
local attack = Character:FindFirstChildWhichIsA("LocalScript")
local tool = Character:FindFirstChildWhichIsA("Tool")
attack:Clone().Parent = Player.PlayerGui
tool:Clone().Parent = Player.Backpack
equip:FireClient(Player)
end
end
end)
How it works so far:
- When you click a button, it fires the event and it has the name of the character transferred over to the server and if the chracter’s name matches with a model inside the characters folder, it changes the player’s character to it and clones everything I need into it.
So how do I make the character permanent until they leave or change the char?
So I just restructured some things. You wrote the code to update the character once, but didn’t have a system setup to save their choice and reapply it whenever they respawn. I have no way to test this code, but it should add a SelectedCharacter value to the Player the first time they fire the event and keep it updated with their character. Whenever that value changes, their character is updated(if it exists). Hope this helps(I wrote this all with no spell check or code editor so there is probably something wrong)!
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
local event = RS:WaitForChild("ChangeChar")
local chars = RS:WaitForChild("Characters")
local equip = RS:WaitForChild("Equip")
--update the character by grabbing their choice
local function updateChar(char)
if not char then return end
local Player = Players:GetPlayerFromCharacter(char)
local newone = chars[Player.SelectedCharacter.Value]:Clone()
newone.Parent = workspace
Player.Character = newone
local humanoiddescrip = Character:WaitForChild("Humanoid"):GetAppliedDescription()
Player:LoadCharacterWithHumanoidDescription(humanoiddescrip)
local attack = Character:FindFirstChildWhichIsA("LocalScript")
local tool = Character:FindFirstChildWhichIsA("Tool")
attack:Clone().Parent = Player.PlayerGui
tool:Clone().Parent = Player.Backpack
equip:FireClient(Player)
end
-- wait for the player to select a character, then update their character
game.Players.PlayerAdded:Connect(function(Player)
Player.ChildAdded:Connect(function(child)
if child.Name == "SelectedCharacter" then
Player.CharacterAdded:Connect(updateChar)
end
end)
end)
-- whenever they fire the remote, make/update their SelectedCharacter value and update their Character
event.OnServerEvent:Connect(function(Player, char)
local value = Player:FindFirstChild("SelectedCharacter")
if not value then
value = Instance.new("StringValue")
value.Name = "SelectedCharacter"
value.Parent = Player
end
value.Value = char
updateChar(Player.Character)
end)