Character variable not usable after dying

when the character first spawns in, it can create the viewmodel just fine. however, if the character dies, the variable stops working and this code block doesnt work.

character.ChildAdded:Connect(function(child)
	print("h")
	if child:IsA("Tool") and child.Name == "M4A1" then
		viewmodel = game.ReplicatedStorage.Viewmodels.Viewmodel:Clone()
		gunmodel = game.ReplicatedStorage.Items.M4A1:Clone()
		
		viewmodel.Parent = camera
		
		gunmodelAnims = gunmodel.Anims
		
		mainmodule.weldGun(gunmodel)
		mainmodule.equip(viewmodel, gunmodel, gunmodelAnims.Hold)
	end
end)

variable

local character = localplayer.Character or localplayer.CharacterAdded:Wait()

I’d suggest you put this local script in the StarterCharacterScripts section. Now, you can refer to the character as script.Parent in that script. Let me know if this works.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.