Health bar doesn't update after character change

So basically, I made a GUI where after clicking on a button, A rig (with a humanoid and animation scripts) is cloned from replicatedstorage to workspace and assigned as your character. (I used player.Character) Other than the fact that I had to add additional script for the camera being linked to the new character’s head, it was all was working smoothly…that is until I realized that the normal health bar doesn’t change when you’re damaged or healed.

Just for extra specification, by health bar, I’m talking about the default roblox one that’s by your name at the top right. I haven’t made my own GUI.

I investigated a bit and tried getting damaged with my own character (roblox avatar) instead of the custom one. The health bar was responding accordingly and it lowered as I took more damage, so this leads me to believe it’s something with either the switch between characters or the custom character itself is the cause for this. If anyone who’s dealt with stuff like this before could give a solution (entire scripts are not needed), that would be great!

If any more information is needed, feel free to ask!

1 Like

when the default player spawns in (before character change) you use player.Character so it gets their old character which contains the healthbar. When you change the character the script is still referencing the old character which i assume gets destroyed. Try referencing the players character after their custom character as been spawned

1 Like

I’m fairly certain you’re right. I tried doing it after the custom character was spawned, but no difference. Thanks for the suggestion though.

can I see your code? I might have a solution

Sure, what code do you want specifically? It’s a bit spread out between a LocalScript and ServerScript

is it possible to get both? the issue might be on either. Just the code that is relevant to the characters health.

1 Like

Alright.
Some extra stuff is that this code is under an ImageButton. There is also a RemoteEvent object under the same imagebutton. The localscript recieves the click and fires the remote event and controls the camera of the player, but the server script does everything else.

Localscript:

 function mouseclick()
	local player = game.Players.LocalPlayer
	script.Parent.RemoteEvent:FireServer()
	script.Parent.Parent.Parent.Enabled = false
	local camera = game.Workspace.CurrentCamera
	local rig = game.Workspace:WaitForChild(player.Name.."2")
	camera.CameraType = "Custom"
	camera.CameraSubject = rig:WaitForChild("Head")
end
script.Parent.MouseButton1Click:Connect(mouseclick)

Serverscript:

local remoteEvent = script.Parent.RemoteEvent
local function suitchange(player)
	local actualplayer = game.Players:WaitForChild(player.Name)
	local clone = game.ReplicatedStorage.SpiderMan:Clone()
	clone.Name = player.Name.."2"
	clone.Parent = game.Workspace
	actualplayer.Character = clone
	player.PlayerGui:FindFirstChild("Character Selection").Enabled = false
	local yes = game.StarterPlayer.StarterCharacterScripts.WallClimb:Clone()
	yes.Parent = clone 
	local webshooter = game.ReplicatedStorage["Web Shooter"]:Clone()
	webshooter.Parent = actualplayer.Backpack
	local webs = game.ReplicatedStorage["Spider-Webs"]:Clone()
	webs.Parent = actualplayer.Backpack
end
remoteEvent.OnServerEvent:Connect(suitchange)
1 Like

this maybe because that the character is still the same, mind looking at the workspace when you change your character?

1 Like

The original character completely gets removed from the game after the character is switched. However, one thing to note might be that the script I have names the new rig the player’s username, but it adds a “2” to the end of the username. The actual name of the player in game.Players remains the same.

For example, if your roblox username was “bot”, the playercharacter would be called “bot2”, but your player would still obviously be called “bot”.

1 Like

if that is the case then if your humanoid changes and its not your character it should be

workspace[Player.Name.."2"].Humanoid.Health
1 Like

Alright. It’s getting very late where I live so I’ll try again tomorrow. I’m going to probably attempt seeing if moving the humanoid from the old character to the new one is possible and if it’ll fix it. Thanks for your help!

1 Like