Hi recently I was trying to code a character selector which switches to different r15 characters along with tools that equip when you press a key instead of using the toolbar I finished coding the tool equip stuff but for some reason when you switch characters the starter gui and starter character scripts don’t switch to the new character and the animations that play when you press a certain key aren’t working along with the tool that is equipped when you press that key.
Here’s my code for character switching:
local Jason = game.ReplicatedStorage.Jason
local plr = game.Players.LocalPlayer
local button = script.Parent
local StarterCharacterScripts = game.StarterPlayer.StarterCharacterScripts:GetChildren()
local LocalScripts = {}
local CharacterSelector = game.StarterGui["Old Character Slector"].Enabled
CharacterSelector = true
local function onButtonActivated()
local oldchar = plr.Character
local customchar = Jason:Clone()
customchar.Name = plr.Name
plr.Character = customchar
oldchar.Health:Clone().Parent = customchar
customchar.Parent = workspace
customchar.PrimaryPart.CFrame = oldchar.PrimaryPart.CFrame
for index2,item2 in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
if item2:IsA('LocalScript') then
table.insert(LocalScripts,item2:Clone())
else
item2:Clone().Parent = customchar
end
end
plr.Team = game.Teams.Jason
oldchar:Destroy()
end
button.Activated:Connect(onButtonActivated)
and here’s some code for the animations and equipping one of the tools:
local Plr = game.Players.LocalPlayer
character = Plr.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Animator = humanoid:WaitForChild("Animator")
local AnimationTrack = Instance.new("Animation")
AnimationTrack.AnimationId = "rbxassetid://10759743372"
local AnimationTrack = Animator:LoadAnimation(AnimationTrack)
AnimationTrack.Priority = Enum.AnimationPriority.Action
AnimationTrack.Looped = false
local UserInPutService = game:GetService("UserInputService")
UserInPutService.InputBegan:Connect(function(input, gp)
if gp then return end
if input.KeyCode == Enum.KeyCode.E then
if Plr.Backpack:FindFirstChild("Bat") then
local tool = Plr.Backpack:FindFirstChild("Bat")
humanoid:EquipTool(tool)
AnimationTrack:Play()
humanoid.WalkSpeed = 0
AnimationTrack.Stopped:Wait()
humanoid:UnequipTools()
humanoid.WalkSpeed = 16
end
end
end)
hey I believe the issue is that the script is in StarterPlayerScripts.
There’s two ways to go about this, the first being;
moving the script to StarterCharacterScripts and changing the character part of the code to the following:
character = script.Parent
your current code of CharacterAdded:Wait() is yielding forever because the character is already added. Just setting character to the parent works because the character is already there, and the script is a child of the character.
the second option is;
leaving the script inside StarterPlayerScripts but changing the way you get the character, it should be moved inside of a function for when there character is added similar to this;
local function OnCharacterAdded(Character)
— do stuff
end
if Player.Character then
OnCharacterAdded(Player.Character)
end
Player.CharacterAdded:Connect(OnCharacterAdded)
though, you likely will have to change portions of the script as this method doesn’t delete/refresh the script itself, only the code for when your character is added.
I might have after reading your post changed every mention of the character from player.Character to script.Parent I tried fixing it and I found that ChracterAdded:Wait() might not be yielding as its the only definition of the character that provides some results as animations play now but with player.Character it doesn’t the only error I’ve gotten was attempt to index nil with 'WaitForChild' which was me trying to get the humanoid of the character. I think I still need to find a proper way to define the character due to the only reason I got that error was how I wrote character = game.Workspace:WaitForChild(player.Name) which hasn’t worked neither has player.Character really. Non of my items can be equipped either because of 1. I was trying to code items for each team like for my character selector after choosing you character you would be put on a team and given the certain items for that team but it doesn’t work and 2. There’s a tool that’s for all teams that when you hold down on a key it equips the tool but other wise it unequips all tools because I frankly don’t know how to unequip one tool so my code was Humanoid:Unequiptools(tool) tool was just local tool = script.Parent ,but I’ve gotten all gui to work and character scripts working inside my new character. Finally I was trying to get character switching in the character selector working but I’m really struggling to code it also sorry for the long paragraph just there was a lot of stuff that came up and I didn’t mention some stuff so I just wanted to clarify. Here’s my updated code for the character switching:
local Jason1 = game.ReplicatedStorage.Jason
local Player = game.Players.LocalPlayer
local button = script.Parent
local StarterCharacterScripts = game.StarterPlayer.StarterCharacterScripts:GetChildren()
local CharacterSelector = game.StarterGui["Old Character Slector"].Enabled
local LocalScripts = {}
local humanoid = Player.Character:WaitForChild("Humanoid")
local walkspeed = humanoid.WalkSpeed
walkspeed = 0
local rs = game:GetService("ReplicatedStorage")
local CanContinue = rs:WaitForChild("CanContinueRe")
local GUI = game.StarterGui["Old Character Slector"]
local function onButtonActivated() -- Checking if the player clicked on the block
local ChosenCharacter = Jason1:Clone()
local CurrentCharacter = Player.Character or workspace:WaitForChild(Player.Name) or Player.CharacterAdded:Wait()
local LocalScripts = {}
CharacterSelector = false
for index2,item2 in pairs(game.StarterPlayer.StarterCharacterScripts:GetChildren()) do
if item2:IsA('LocalScript') then
table.insert(LocalScripts,item2:Clone())
else
item2:Clone().Parent = ChosenCharacter
end
end
CurrentCharacter.Health:Clone().Parent = ChosenCharacter
table.insert(LocalScripts,CurrentCharacter.Animate:Clone())
ChosenCharacter.Parent = workspace
ChosenCharacter.Name = Player.Name
Player.Character = ChosenCharacter
ChosenCharacter.PrimaryPart.CFrame = CurrentCharacter.PrimaryPart.CFrame
GUI.Parent = game.Players.LocalPlayer.PlayerGui
CurrentCharacter:Destroy()
--script.Parent:GetChildren():Isa("Frame").Visible = false
for index2,item2 in pairs(LocalScripts) do
item2.Parent = ChosenCharacter
end
end
button.Activated:Connect(onButtonActivated)
Because what I wrote is very long and the main problem is solved, I’m gonna make a few more topics to solve these new problems that came up. Also I solved the problem by rewriting the way the character changes and I learned how to grab the starter gui and put it in the character the whole script is here: